Python Articles

Page 146 of 855

Program to build and evaluate an expression tree using Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 2K+ Views

Suppose, we are given the postfix traversal of an expression tree. We have to build an expression tree from the given postfix traversal, and then evaluate the expression. We return the root of the expression tree and the evaluated value of the tree. So, if the input is like: * + + ...

Read More

Program to find out if two expression trees are equivalent using Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 296 Views

When working with expression trees, we sometimes need to determine if two trees represent equivalent expressions. This involves checking whether both trees evaluate to the same result, even if their structure differs due to commutative properties of operations. Expression trees store mathematical expressions where leaf nodes contain operands and internal nodes contain operators. Two trees are equivalent if they produce the same value when evaluated. .node { fill: lightblue; stroke: black; stroke-width: 2; } .leaf { fill: ...

Read More

Program to find out the node in the right in a binary tree using Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 578 Views

Sometimes we need to find the node immediately to the right of a given node in a binary tree. The right node must be at the same level as the target node. This problem can be solved using level-order traversal (BFS) to process nodes level by level. So, if the input is like 5 3 7 2 ...

Read More

Program to find latest group of size M using Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 257 Views

Suppose we have an array arr holding a permutation of numbers from 1 to n. We have a binary string of size n with all bits initially set to zero. At each step i (1-indexed), we set the bit at position arr[i] to 1. Given a value m, we need to find the latest step at which there exists a group of ones of size exactly m. A group of ones means a contiguous substring of 1s that cannot be extended in either direction. If no such group exists, we return -1. Example Walkthrough If the input ...

Read More

Program to find minimum numbers of function calls to make target array using Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 355 Views

Suppose we have a function that can perform two operations on an array: def modify(arr, op, index): if op == 0: arr[index] += 1 if op == 1: for i in range(len(arr)): arr[i] *= 2 We need to find the minimum number of function calls required to transform a zero array into a given target array nums. Problem Understanding ...

Read More

Program to find minimum number of vertices to reach all nodes using Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 549 Views

Suppose we have a directed acyclic graph with n vertices numbered from 0 to n-1. The graph is represented by an edge list, where edges[i] = (u, v) represents a directed edge from node u to node v. We need to find the smallest set of vertices from which all nodes in the graph are reachable. The key insight is that nodes with no incoming edges cannot be reached from any other nodes, so they must be included in our starting set. Example Graph 0 ...

Read More

Program to find minimum operations to make array equal using Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 329 Views

Suppose we have a value n, consider an array nums with n elements, where arr[i] = (2*i)+1 for all i. Now in one operation, we can choose two indices x and y where 0 = 0, add (n-j) to ans, decrement q, and increment j by 2 Return ans Example def solve(n): ans = 0 if n == 1: return ans q = (n // 2) - 1 j = 1 ...

Read More

Program to find maximum number of non-overlapping subarrays with sum equals target using Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 253 Views

Given an array of numbers and a target value, we need to find the maximum number of non-overlapping subarrays where each subarray's sum equals the target. For example, if nums = [3, 2, 4, 5, 2, 1, 5] and target = 6, we can find two subarrays: [2, 4] and [1, 5], both with sum 6. Approach Using Prefix Sum and Set The key insight is to use prefix sums and track cumulative sums in a set. When we find a valid subarray, we reset our tracking to avoid overlaps. Algorithm Steps Initialize a ...

Read More

Program to find Kth bit in n-th binary string using Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 461 Views

Suppose we have two positive values n and k. We can construct a binary string S_n using the following rules: S_1 = "0" S_i = S_(i-1) concatenate "1" concatenate reverse(invert(S_(i-1))) for i > 1 Here reverse(x) returns the reversed string x, and invert(x) flips all the bits in x (0 becomes 1, 1 becomes 0). Binary String Generation Pattern Let's see how these strings are constructed: S_1 = "0" S_2 = "011" S_3 = "0111001" ...

Read More

Program to check whether we can convert string in K moves or not using Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 271 Views

In this problem, we need to determine if we can convert string s to string t using at most k moves. In the i-th move, we can select an unused index and shift the character at that position i times forward in the alphabet (with wrapping from 'z' to 'a'). Problem Understanding The key insight is that in move i, we can shift a character by exactly i positions. Since the alphabet wraps around, we need to calculate how many times each required shift distance can be achieved within k moves. Algorithm Steps Check if ...

Read More
Showing 1451–1460 of 8,549 articles
« Prev 1 144 145 146 147 148 855 Next »
Advertisements