Arnab Chakraborty

Arnab Chakraborty

3,768 Articles Published

Articles by Arnab Chakraborty

Page 74 of 377

Check if array elements are consecutive in O(n) time and O(1) space (Handles Both Positive and negative numbers) in Python

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

We need to check if an array contains consecutive integers in O(n) time and O(1) space complexity. This algorithm works with both positive and negative numbers by using the arithmetic progression sum formula. Algorithm Approach The solution uses the mathematical property that consecutive integers form an arithmetic progression with common difference 1. We can verify if numbers are consecutive by comparing the actual sum with the expected sum of an arithmetic progression. Step-by-Step Process Find the minimum element (first term of AP) Calculate expected sum using AP formula: n/2 × (2a + (n-1)d) where d=1 ...

Read More

Check if any permutation of N equals any power of K in Python

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

This problem asks us to determine if any permutation of the digits of number n equals some power of number m. For example, given n = 7182 and m = 12, we find that 1728 (a permutation of 7182) equals 12³. Approach We'll generate all powers of m within our range, then check if any power has the same digit frequency as n using sets for comparison ? Helper Function to Check Digit Permutation First, we create a function to check if two numbers have the same digits ? def check_power(n, m): temp_arr_1 = [] ...

Read More

Check if any large number is divisible by 17 or not in Python

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

Checking if a large number is divisible by 17 can be efficiently done using a repeated subtraction method. Instead of performing direct division, we extract the last digit and subtract it multiplied by 5 from the remaining number until we get a manageable two-digit number. The algorithm works because of the mathematical property that a number is divisible by 17 if and only if the number formed by removing the last digit and subtracting 5 times the last digit is also divisible by 17. Algorithm Steps To solve this problem, we follow these steps: While ...

Read More

Check if an integer can be expressed as a sum of two semi-primes in Python

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

A semi-prime number is a positive integer that can be expressed as the product of exactly two prime numbers (not necessarily distinct). For example, 4 = 2×2, 6 = 2×3, and 9 = 3×3 are semi-primes. This problem asks us to determine if a given integer n can be expressed as the sum of two semi-prime numbers. Understanding Semi-Prime Numbers The first few semi-prime numbers in the range 1-100 are: 4, 6, 9, 10, 14, 15, 21, 22, 25, 26, 33, 34, 35, 38, 39, 46, 49, 51, 55, 57, 58, 62, 65, 69, 74, 77, 82, ...

Read More

Check if an encoding represents a unique binary string in Python

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

Suppose we have an array called nums that represents an encoding of a binary string of size k. We need to check whether the given encoding uniquely identifies a binary string or not. The encoding contains counts of contiguous 1s which are separated by single 0s. So, if the input is like nums = [4, 2, 3] and k = 11, then the output will be True because there is a binary string like 11110110111 of length k = 11. Understanding the Problem The encoding works as follows ? Each number in nums represents a ...

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 231 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 all the 1s in a binary string are equidistant or not in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 237 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 294 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 189 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
Showing 731–740 of 3,768 articles
« Prev 1 72 73 74 75 76 377 Next »
Advertisements