When working with numbers, we sometimes need to check if all sub-numbers have unique digit products. A sub-number is any contiguous sequence of digits from the original number. For example, the sub-numbers of 135 are 1, 3, 5, 13, 35, 135. The digit product of a number is the product of all its individual digits. For a number with n digits, there are n*(n+1)/2 possible sub-numbers. We need to calculate the digit product for each sub-number and verify that all products are distinct. Example Problem If the input is n = 235, the sub-numbers are [2, 3, ... Read More
Suppose we have n people and two identical voting machines. We have an array called time where time[i] represents the total time needed by the i-th person to vote on any machine. Only one person can use each machine at a time. Given a maximum allowable time x for which machines are operational, we need to check whether all people can vote within this time limit. For example, if n = 3, x = 7, and time = [3, 5, 3], the answer is True. At time t0, person 0 uses machine 1 and person 1 uses machine 2. ... Read More
Sometimes we need to check if all occurrences of a specific character appear consecutively in a string. This means the character should form a single continuous block without being scattered throughout the string. For example, in the string "bbbbaaaaaaaccddd", all occurrences of 'a' appear together as one block, so the result is True. However, in "bbbaaacccaaaddd", the character 'a' appears in two separate groups, so the result would be False. Algorithm Approach We can solve this using a flag-based approach ? Use a flag to track if we've already seen a block of the target ... Read More
In this problem, we need to determine if all enemies in a matrix can be eliminated by bombs. The matrix contains three types of cells: 0 for empty area 1 for bomb 2 for enemies Bombs can blast horizontally and vertically across the entire row and column, destroying all enemies in their path (but not other bombs). Problem Example Consider this matrix: 0020 0100 0200 0010 The bomb at position [1, 1] can eliminate the enemy at [2, 1], and the bomb at [3, 2] can eliminate the enemy at ... Read More
A palindrome array is an array that reads the same forwards and backwards. In Python, we can check if an array is a palindrome by comparing elements from both ends moving towards the center. So, if the input is like nums = [10, 12, 15, 12, 10], then the output will be True because the array reads the same from left to right and right to left. Algorithm To solve this, we will follow these steps ? Get the size of the array Use a flag variable ... Read More
Suppose we have a number n. We have to check whether all digits of it can divide n or not. So, if the input is like n = 135, then the output will be True, because (135 / 1 = 135), (135 / 3 = 45) and (135 / 5 = 27). Algorithm To solve this, we will follow these steps − val := n while val > 0, do ... Read More
In computers, data is stored in the binary form, which uses the two digits 0 and 1. Each digit in this format is known as a bit. In this article, we are going to check if all bits of a number are set in Python. A bit is said to be set if the binary representation of every bit is 1. Let's look at some scenarios to understand this better: Scenario 1 Input: 3 Binary: 11 Output: True Explanation: All bits are 1. Scenario 2 Input: 6 Binary: 110 Output: False ... Read More
Given a two-character string s and a list of two-character words w, we need to check whether we can concatenate words from w to create a string that contains s as a substring. For example, if s = "no" and w = ["ol", "on", "ni", "to"], we can concatenate "on" + "ol" to get "onol", which contains "no" as a substring. Algorithm To solve this problem, we need to check three possible scenarios ? The target string s exists directly in the word list We can form s by taking the second character of one ... Read More
In geometry, a triangle with positive area must satisfy two conditions: the sum of all angles must equal 180 degrees, and each angle must be positive (greater than 0). Let's explore how to check if three given angles can form a valid triangle. Problem Understanding Given three angles, we need to verify if they can form a triangle with positive area. For a valid triangle: All angles must be positive (greater than 0) Sum of all three angles must equal 180 degrees Each angle must be less than 180 degrees (since other two angles must be ... Read More
Sometimes when we type on a keyboard, vowel keys might get long pressed, causing them to repeat one or more times. Given two lowercase strings s (the intended name) and t (the typed name), we need to check whether t could be a result of typing s with some vowels accidentally repeated. So, if the input is like s = "mine" and t = "miiine", then the output will be True because the vowel 'i' is repeated three times while other letters remain unchanged. Algorithm Steps To solve this problem, we will follow these steps − ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Economics & Finance