Programming Articles

Page 472 of 2547

Check if an array represents Inorder of Binary Search tree or not in Python

Yaswanth Varma
Yaswanth Varma
Updated on 25-Mar-2026 656 Views

The Binary Search Tree (BST) is a widely used data structure that maintains elements in a sorted hierarchical order. Each node in the BST follows a specific property: The values in the left subtree are always less than the current node value. The values in the right subtree are always greater than the current node value. In this article, we will learn how to check if an array represents the inorder traversal of a BST in Python. Understanding BST Inorder Traversal Inorder traversal is a common ...

Read More

Check if an array of 1s and 2s can be divided into 2 parts with equal sum in Python

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

Suppose we have an array nums which only stores 1 and 2 in it. We have to check whether the array can be divided into two different parts such that sum of elements in each part is same. So, if the input is like nums = [1, 1, 2, 2, 2], then the output will be True as we can divide this array like [1, 1, 2] and [2, 2] − the sum of each part is 4. Algorithm To solve this, we will follow these steps − Calculate total sum ...

Read More

Check if an array contains all elements of a given range in Python

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

Suppose we have an array called nums and two numbers x and y defining a range [x, y]. We need to check whether the array contains all elements in the given range or not. So, if the input is like nums = [5, 8, 9, 6, 3, 2, 4], x = 2, y = 6, then the output will be True as the array contains all elements [2, 3, 4, 5, 6] from the range. Using Set Intersection Method The simplest approach is to convert the array to a set and check if all range elements are ...

Read More

Check if an array can be divided into pairs whose sum is divisible by k in Python

Yaswanth Varma
Yaswanth Varma
Updated on 25-Mar-2026 351 Views

Given an array of integers and a number k, we need to determine whether it's possible to divide the entire array into pairs such that the sum of every pair is divisible by k. For example, with array [2, 4, 1, 3] and k = 5: Pair (2, 3) → 2 + 3 = 5 (divisible by 5) Pair (4, 1) → 4 + 1 = 5 (divisible by 5) Result: True (all pairs sum to multiples of k) Algorithm The key insight is that for two numbers to sum to a multiple of ...

Read More

Check if all the 1s in a binary string are equidistant or not in Python

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

Suppose we have a binary string, we need to check whether all the 1s in the string are equidistant or not. In other words, the distance between every two consecutive 1s should be the same, and the string must contain at least two 1s. So, if the input is like s = "100001000010000", then the output will be True as the 1s are at positions 0, 5, 10 with equal distances of 5 between them. Algorithm To solve this problem, we will follow these steps − Find all indices where '1' appears in the string ...

Read More

Check if all sub-numbers have distinct Digit product in Python

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

When working with numbers, we sometimes need to check if all sub-numbers have unique digit products. A sub-number is any contiguous sequence of digits from the original number. For example, the sub-numbers of 135 are 1, 3, 5, 13, 35, 135. The digit product of a number is the product of all its individual digits. For a number with n digits, there are n*(n+1)/2 possible sub-numbers. We need to calculate the digit product for each sub-number and verify that all products are distinct. Example Problem If the input is n = 235, the sub-numbers are [2, 3, ...

Read More

Check if all people can vote on two machines in Python

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

Suppose we have n people and two identical voting machines. We have an array called time where time[i] represents the total time needed by the i-th person to vote on any machine. Only one person can use each machine at a time. Given a maximum allowable time x for which machines are operational, we need to check whether all people can vote within this time limit. For example, if n = 3, x = 7, and time = [3, 5, 3], the answer is True. At time t0, person 0 uses machine 1 and person 1 uses machine 2. ...

Read More

Check if all occurrences of a character appear together in Python

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

Sometimes we need to check if all occurrences of a specific character appear consecutively in a string. This means the character should form a single continuous block without being scattered throughout the string. For example, in the string "bbbbaaaaaaaccddd", all occurrences of 'a' appear together as one block, so the result is True. However, in "bbbaaacccaaaddd", the character 'a' appears in two separate groups, so the result would be False. Algorithm Approach We can solve this using a flag-based approach ? Use a flag to track if we've already seen a block of the target ...

Read More

Check if all enemies are killed with bombs placed in a matrix in Python

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

In this problem, we need to determine if all enemies in a matrix can be eliminated by bombs. The matrix contains three types of cells: 0 for empty area 1 for bomb 2 for enemies Bombs can blast horizontally and vertically across the entire row and column, destroying all enemies in their path (but not other bombs). Problem Example Consider this matrix: 0020 0100 0200 0010 The bomb at position [1, 1] can eliminate the enemy at [2, 1], and the bomb at [3, 2] can eliminate the enemy at ...

Read More

Check if all elements of the array are palindrome or not in Python

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

A palindrome array is an array that reads the same forwards and backwards. In Python, we can check if an array is a palindrome by comparing elements from both ends moving towards the center. So, if the input is like nums = [10, 12, 15, 12, 10], then the output will be True because the array reads the same from left to right and right to left. Algorithm To solve this, we will follow these steps ? Get the size of the array Use a flag variable ...

Read More
Showing 4711–4720 of 25,466 articles
« Prev 1 470 471 472 473 474 2547 Next »
Advertisements