Arnab Chakraborty

Arnab Chakraborty

3,768 Articles Published

Articles by Arnab Chakraborty

Page 71 of 377

Check if the given decimal number has 0 and 1 digits only in Python

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

Sometimes we need to check if a decimal number contains only binary digits (0 and 1). This is useful for validating binary representations or checking if a number can be interpreted as a binary string. So, if the input is like num = 101101, then the output will be True since it contains only 0s and 1s. Algorithm To solve this, we will follow these steps − Extract all digits from the number into a set Remove 0 and 1 from the digits set If the set is empty after removal, return True Otherwise, return ...

Read More

Check if the given array contains all the divisors of some integer in Python

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

Sometimes we need to verify if a given array contains all the divisors of some integer. This problem involves finding divisors of the maximum element and comparing them with the array elements. So, if the input is like nums = [1, 2, 3, 4, 6, 8, 12, 24], then the output will be True as these are all the divisors of 24. Algorithm To solve this, we will follow these steps − Find the maximum element in the array (potential number whose divisors we're checking) Generate all ...

Read More

Check if the given array can be reduced to zeros with the given operation performed given number of times in Python

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

We need to check if an array can be reduced to all zeros by performing a specific operation exactly k times. The operation subtracts the smallest non-zero element from all non-zero elements in the array. Understanding the Problem Let's break down how the operation works ? Operation: Find the smallest non-zero element and subtract it from all non-zero elements Goal: Reduce all elements to zero in exactly k operations Key insight: Each operation eliminates one distinct value from the array Example Walkthrough Consider the array [2, 2, 3, 5] with k = 3 ...

Read More

Check if the frequency of all the digits in a number is same in Python

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

A number is considered balanced when the frequency of all its digits is the same. For example, in the number 562256, each digit (5, 6, and 2) appears exactly 2 times, making it a balanced number. In this article, we'll explore different methods to check if a number is balanced in Python. Using Dictionary to Count Frequencies We can convert the number to a string and use a dictionary to count the frequency of each digit ? from collections import defaultdict def is_balanced(num): number = str(num) ...

Read More

Check if the first and last digit of the smallest number forms a prime in Python

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

Given an array of digits, we need to find the smallest possible number from those digits, then check if numbers formed by the first and last digits are prime. This involves sorting digits to create the minimum number and testing primality. Problem Overview For example, if we have digits = [5, 2, 1, 7], the smallest number is 1257. We then form numbers using first and last digits: 17 and 71, and check if they are prime. Algorithm Steps The solution follows these steps − Count frequency of each digit (0-9) Build the smallest ...

Read More

Check if the elements of stack are pairwise sorted in Python

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

Suppose we have a stack of numbers; we have to check whether values in the stack are pairwise consecutive or not. These pairs can be increasing or decreasing. If the stack has an odd number of values, the top element is left out of a pair. We should retain the original stack content after checking. To solve this problem, we can use basic stack operations: push, pop and check if the stack is empty. Example If the input is like stk = [5, 6, -4, -5, 12, 11, 6, 7, 22], then the output will be True. ...

Read More

Check if the characters of a given string are in alphabetical order in Python

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

Suppose we have a string s. We have to check whether characters in s are in alphabetical order or not. So, if the input is like s = "mnnooop", then the output will be True. Method 1: Using Sorting Approach To solve this, we will follow these steps ? char_arr := a new list from the characters present in s sort the list char_arr return char_arr is same as list of all characters in s then true otherwise false Example def solve(s): char_arr = list(s) ...

Read More

Check if the characters in a string form a Palindrome in O(1) extra space in Python

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

A palindrome reads the same forwards and backwards. When checking if a string forms a palindrome, we often need to ignore non-letter characters and focus only on alphabetic characters. This problem requires solving it in O(1) extra space, meaning we cannot create additional data structures. So, if the input is like s = "ra$5ce58car", then the output will be True, as the letters form "racecar" which is a palindrome. Algorithm We use a two-pointer approach with helper functions to find the next valid letter from both ends ? Use two pointers: left (start) and right ...

Read More

Check if the binary representation of a number has equal number of 0s and 1s in blocks in Python

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

When working with binary representations, we sometimes need to check if consecutive blocks of 0s and 1s have the same length. This means that all groups of consecutive identical bits should have equal sizes. For example, if we have the number 455, its binary representation is 111000111. Here we have three consecutive 1s, followed by three consecutive 0s, followed by three consecutive 1s − all blocks have the same length of 3. Problem Statement Given a number, we need to check whether its binary representation has the same number of consecutive blocks of 0s and 1s. Note ...

Read More

Check if the array is beautiful in Python

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

A beautiful array is defined as an array of unique elements that satisfies two specific conditions. In this tutorial, we'll learn how to check if an array meets the criteria for being "beautiful" in Python. Conditions for a Beautiful Array An array is considered beautiful if it meets these requirements: All elements are in the range 1 to n (where n is the array length) The array is not sorted in ascending order All elements are unique (no duplicates) Algorithm Steps To check if an array is beautiful, we follow these steps: ...

Read More
Showing 701–710 of 3,768 articles
« Prev 1 69 70 71 72 73 377 Next »
Advertisements