Python Articles

Page 646 of 855

Check if a king can move a valid move or not when N nights are there in a modified chessboard in Python

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

Suppose we have an infinite chessboard with the same rules as chess. Given N knights and a king's position, we need to check whether the king is in checkmate or not. The coordinate system is bounded by large values like (-10^9

Read More

Check if a key is present in every segment of size k in an array in Python

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

Suppose we have an array A with N elements, we have another value p and a segment size k, we have to check whether key p is present in every segment of size k in A. So, if the input is like A = [4, 6, 3, 5, 10, 4, 2, 8, 4, 12, 13, 4], p = 4 and k = 3, then the output will be True because: Segment 1: [4, 6, 3] contains 4 ✓ Segment 2: [5, 10, 4] contains 4 ✓ Segment 3: [2, 8, 4] contains 4 ✓ Remaining elements: [12, ...

Read More

Check if a given Binary Tree is Heap in Python

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

A binary heap is a special binary tree with two key properties: it must be a complete tree (all levels filled except possibly the last, which fills left to right), and it must satisfy the heap property (each parent node is greater than or equal to its children for a max-heap). In this article, we'll implement a solution to check if a given binary tree satisfies both heap properties. Understanding Heap Properties A binary tree is a valid max-heap if: Complete Tree Property: All levels are completely filled except possibly the ...

Read More

Check if a given Binary Tree is height balanced like a Red-Black Tree in Python

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

A Red-Black Tree is a balanced binary search tree where the height difference is controlled. In such trees, the longest path from any node to a leaf is at most twice the length of the shortest path. This property ensures the tree remains balanced and provides efficient operations. We need to check if a given binary tree satisfies this height-balanced property where for every node, the longest leaf-to-node path is not more than double the shortest path from node to leaf. Problem Understanding Given a binary tree like this: ...

Read More

Check for balanced parentheses in an expression O(1) space O(N^2) time complexity in Python

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

Checking for balanced parentheses is a classic programming problem. While the typical stack-based approach uses O(n) space, this article presents an O(1) space solution with O(n²) time complexity that modifies the string in-place by marking processed characters. Algorithm Overview The algorithm uses three global variables to track state and processes closing brackets by finding their matching opening brackets ? cnt ? counts unmatched opening brackets i ? current position in the string j ? position of the last unmatched opening bracket How It Works When a closing bracket is found, the solve() ...

Read More

Find the Largest Cube formed by Deleting minimum Digits from a number in Python

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

Suppose we have a number N, we have to determine the largest perfect cube that can be generated by removing minimum digits (possibly 0) from the number. We can delete any digit from the given number to reach the target. As we know a number N is called a perfect cube if N = M³ for some integer M. So, if the input is like 806, then the output will be 8, as we can delete 0 and 6 from the number, then we will get 8, which is perfect cube of 2. Algorithm To solve this, ...

Read More

Find the largest Complete Subtree in a given Binary Tree in Python

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

A complete binary tree is one where all levels are completely filled except possibly the last level, and the last level has all keys as far left as possible. In this problem, we need to find the largest complete subtree within a given binary tree and return its size. So, if the input is like ? 50 30 60 5 ...

Read More

Find a pair from the given array with maximum nCr value in Python

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

When working with arrays, we sometimes need to find a pair of elements that maximizes the binomial coefficient nCr value. Given an array of integers, we want to find arr[i] and arr[j] such that arr[i]C arr[j] is as large as possible. The key insight is that for a given n, nCr is maximized when r is closest to n/2. This mathematical property helps us efficiently find the optimal pair. Example If the input array is [4, 1, 2], we need to find the pair with maximum nCr value ? 4C1 = 4 4C2 = 6 ...

Read More

Minimum removals from array to make GCD Greater in Python

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

When we have a list of numbers, we may need to remove some elements to increase the GCD (Greatest Common Divisor) of the remaining numbers. This problem asks for the minimum number of removals required to make the GCD larger than the original GCD. For example, given [6, 9, 15, 30], the initial GCD is 3. After removing 6 and 9, we get [15, 30] with GCD = 15, which is greater than 3. So the answer is 2 removals. Algorithm Approach The solution uses prime factorization and the Sieve of Eratosthenes ? Find the ...

Read More

Find three element from different three arrays such that that a + b + c = sum in Python

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

Finding three elements from different arrays that sum to a target value is a common programming problem. We need to check if there exist elements a, b, c from arrays A, B, C respectively such that a + b + c equals the given sum. So, if the input is like A = [2, 3, 4, 5, 6], B = [3, 4, 7, 2, 3], C = [4, 3, 5, 6, 7], sum = 12, then the output will be True as 4+2+6 = 12, where 4, 2, 6 are taken from A, B, C respectively. Method 1: ...

Read More
Showing 6451–6460 of 8,546 articles
« Prev 1 644 645 646 647 648 855 Next »
Advertisements