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
In many programming problems, intervals are used to represent ranges such as time periods, numeric spans, etc. An interval is typically represented as a pair of numbers (start, end) where start ≤ end. What is Complete Interval Overlap? Complete overlap occurs when one interval fully contains another. For intervals X = (a1, a2) and Y = (b1, b2), X completely overlaps Y when a1 ≤ b1 and a2 ≥ b2. This means the entire range of Y lies within X. .label { ... Read More
An anagram is a rearrangement of the characters of a word or phrase to generate a new word, using all the original characters exactly once. For example, thing and night are anagrams of each other. A palindrome is a word or phrase that reads the same forward and backward, like madam or racecar. In this article, we'll check if any anagram of a string can form a palindrome. The key insight is that for a string to have a palindromic anagram, at most one character can have an odd frequency. Algorithm Logic For a palindrome to be ... Read More
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
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
The Binary Search Tree (BST) is a widely used data structure that maintains elements in a sorted hierarchical order. Each node in the BST follows a specific property: The values in the left subtree are always less than the current node value. The values in the right subtree are always greater than the current node value. In this article, we will learn how to check if an array represents the inorder traversal of a BST in Python. Understanding BST Inorder Traversal Inorder traversal is a common ... Read More
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
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
Given an array of integers and a number k, we need to determine whether it's possible to divide the entire array into pairs such that the sum of every pair is divisible by k. For example, with array [2, 4, 1, 3] and k = 5: Pair (2, 3) → 2 + 3 = 5 (divisible by 5) Pair (4, 1) → 4 + 1 = 5 (divisible by 5) Result: True (all pairs sum to multiples of k) Algorithm The key insight is that for two numbers to sum to a multiple of ... Read More
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
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Economics & Finance