Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Programming Articles
Page 470 of 2547
Check if bitwise AND of any subset is power of two in Python
Suppose we have an array of numbers called nums. We need to check whether there exists any subset of nums whose bitwise AND is a power of two or not. So, if the input is like nums = [22, 25, 9], then the output will be True, as subset {22, 9} has binary forms {10110, 1001} and their AND is 10000 = 16, which is a power of 2. Understanding the Problem A number is a power of 2 if it has only one bit set (like 1, 2, 4, 8, 16, etc.). We can check this ...
Read MoreCheck if bits of a number has count of consecutive set bits in increasing order in Python
When working with binary representations, we sometimes need to check if consecutive set bits (1s) appear in increasing order. This problem asks us to verify whether groups of continuous 1s in a number's binary representation have counts that increase from left to right. For example, if we have n = 1775, its binary representation is 11011101111. The groups of consecutive 1s are [2, 3, 4], which are in increasing order, so the result is True. Algorithm Steps To solve this problem, we follow these steps: Convert the number to its binary representation Count consecutive groups ...
Read MoreCheck if bits in range L to R of two numbers are complement of each other or not in Python
Suppose we have two numbers x and y and a given range (left, right). We have to check whether all bits in range left to right in both the given numbers are complement of each other or not. We have to keep in mind that from right to left, so the least significant bit is considered to be at first position. So, if the input is like x = 41 y = 54 left = 2 right = 5, then the output will be True, as binary representation of 41 and 54 are 101001 and 110110. The bits in ...
Read MoreCheck if binary string multiple of 3 using DFA in Python
A Deterministic Finite Automaton (DFA) can efficiently check if a binary string represents a number divisible by 3. The DFA uses three states representing remainders 0, 1, and 2 when dividing by 3. So, if the input is like n = [1, 1, 0, 0] (binary of 12), then the output will be True. DFA Structure The DFA has three states corresponding to remainders when dividing by 3 ? 0 1 ...
Read MoreCheck if binary representation of a number is palindrome in Python
Checking if a number's binary representation is a palindrome involves converting the number to binary and verifying if it reads the same forwards and backwards. Python provides several approaches to accomplish this task. Understanding the Problem A binary palindrome reads the same from left to right as from right to left. For example, the number 9 has binary representation "1001", which is a palindrome. Method 1: Using String Conversion The simplest approach is to convert the number to binary string and check if it's equal to its reverse − def is_binary_palindrome_string(n): ...
Read MoreCheck if array sum can be made K by three operations on it in Python
Given an array of numbers and a target value K, we need to check if we can transform the array to have sum K by applying exactly one of three operations to each element: Make the number negative Add its index (1-based) to the number Subtract its index from the number For example, with nums = [1, 2, 3, 7] and k = 8, we can subtract indices from elements 2 and 3 to get [1, 0, 0, 7] with sum 8. Approach Using Dynamic Programming We use memoization to explore all possible combinations ...
Read MoreCheck if array elements are consecutive in O(n) time and O(1) space (Handles Both Positive and negative numbers) in Python
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 MoreCheck if array contains contiguous integers with duplicates allowed in Python
A sequence of numbers is said to be contiguous if the numbers can be arranged in such a way that each number follows the previous one without any gaps. For example, [11, 12, 13, 14] is a contiguous sequence because there are no missing numbers between the smallest and largest values. In this article, we need to check if an array contains such contiguous integers, even when duplicates are present. For instance, [11, 12, 13, 13, 14] should still be considered contiguous because the unique numbers are [11, 12, 13, 14], which form a proper sequence without breaks. ...
Read MoreCheck if any permutation of N equals any power of K in Python
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 MoreCheck if array can be sorted with one swap in Python
Sorting is a task used to organize numbers in increasing order. Usually, we use sorting algorithms to do this, but in most cases, the array is almost sorted, with only two or three numbers in the wrong position. In such scenarios, instead of sorting the entire array, we can check if swapping just one pair of elements will make the entire array sorted. Problem Definition Given an array with distinct integers, we need to find if it can become completely sorted by swapping only one pair of elements. We are not performing the swap, just checking if ...
Read More