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
Articles by Arnab Chakraborty
Page 73 of 377
Check if Decimal representation of an Octal number is divisible by 7 in Python
We need to check whether the decimal representation of a given octal number is divisible by 7. An octal number uses base 8, so we convert it to decimal and then check divisibility. For example, if the octal number is 61, its decimal representation is 6×8¹ + 1×8⁰ = 48 + 1 = 49, which is divisible by 7. Algorithm To solve this problem, we follow these steps − Initialize sum = 0 While the octal number is non-zero: ...
Read MoreCheck if concatenation of two strings is balanced or not in Python
Suppose we have two bracket sequences s and t containing only parentheses '(' and ')'. We need to check whether concatenating these strings in either order (s + t or t + s) results in a balanced parentheses string. So, if the input is like s = "()()))", t = "()(()(", then the output will be True because if we concatenate t + s, we get "()(()(()()))", which is balanced. Algorithm To solve this problem, we follow these steps ? Create a function is_balanced_parenthesis() to check if a string has balanced parentheses Use a stack ...
Read MoreCheck if characters of one string can be swapped to form other in Python
Sometimes we need to check if we can rearrange the characters of one string to form another string. This is essentially checking if two strings are anagrams of each other. So, if the input is like s = "worldlloeh" and t = "helloworld", then the output will be True as we can swap characters from "worldlloeh" to make "helloworld". Algorithm To solve this, we will follow these steps − Check if both strings have the same length Count the frequency of each character in the first string For each character in the second string, decrease ...
Read MoreCheck if both halves of the string have at least one different character in Python
When working with strings, sometimes we need to check if two halves have different character compositions. This involves splitting a string from the middle and comparing character frequencies between the left and right halves. For odd-length strings, we ignore the middle character. Problem Understanding Given a string, we need to ? Split the string into two equal halves (ignoring middle character if odd length) Count character frequencies in each half Check if any character has different frequencies between halves Algorithm Steps The solution follows these steps ? Create frequency maps for ...
Read MoreCheck 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 More