Articles on Trending Technologies

Technical articles with clear explanations and examples

Rotate Matrix in Python

SaiKrishna Tavva
SaiKrishna Tavva
Updated on 25-Mar-2026 7K+ Views

Rotating a matrix in Python can be done using various approaches. The most common method is the Transpose and Reverse technique, which converts rows to columns and then reverses each row to achieve a 90-degree clockwise rotation. Original Matrix Let's consider a 3×3 matrix that we want to rotate 90 degrees clockwise ? 1 5 7 9 6 3 2 1 3 Using Transpose and Reverse This method is efficient and involves two main steps ? Transpose the matrix ? Convert rows to columns and ...

Read More

Check if each internal node of a BST has exactly one child in Python

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

Suppose we have the preorder traversal of a binary search tree (BST). We have to check whether each internal node has only one child or not. So, if the input is like preorder = [22, 12, 13, 15, 14], then the output will be True as BST is like − 22 12 13 15 14 ...

Read More

Check if difference of areas of two squares is prime in Python

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

When working with two numbers, we can check if the difference of their square areas is a prime number. This problem uses the algebraic identity that x² - y² = (x + y)(x - y). For example, if x = 7 and y = 6, the difference is 49 - 36 = 13, which is prime. Understanding the Mathematical Approach The key insight is using the factorization: x² - y² = (x + y)(x - y) For this product to be prime, one factor must equal 1 (since a prime has only two factors: 1 and ...

Read More

Check if Decimal representation of an Octal number is divisible by 7 in Python

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

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 More

Check if concatenation of two strings is balanced or not in Python

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

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 More

Check if characters of one string can be swapped to form other in Python

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

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 More

Check if both halves of the string have at least one different character in Python

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

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 More

Check if bitwise AND of any subset is power of two in Python

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

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 More

Check if bits of a number has count of consecutive set bits in increasing order in Python

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

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 More

Check if bits in range L to R of two numbers are complement of each other or not in Python

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

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 More
Showing 5751–5760 of 61,299 articles
« Prev 1 574 575 576 577 578 6130 Next »
Advertisements