Programming Articles

Page 370 of 2547

Program to add two polynomials given as linked lists using Python

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

Suppose we are given two polynomials represented as linked lists and we need to find their addition. Each polynomial is stored as a linked list where each node contains a coefficient, power, and pointer to the next node. We need to return a new linked list representing the sum of the two polynomials. For example, if we have polynomials 1x^1 + 1x^2 and 2x^1 + 3x^0, then the output will be 3x^1 + 1x^2 + 3x^0. Algorithm To solve this, we will follow these steps − Create dummy and node pointers ...

Read More

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 301 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 583 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 265 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 362 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 558 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 341 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 257 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 468 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
Showing 3691–3700 of 25,466 articles
« Prev 1 368 369 370 371 372 2547 Next »
Advertisements