Suppose we have a list of numbers called nums and also have a target value, we have to find the sum of the largest pair of numbers in nums whose sum is at most (target-1). So, if the input is like nums = [8, 3, 4, 9, 2] target = 8, then the output will be 7, because the sum of the largest pair of numbers less than 8 is 4 + 3 = 7. Algorithm To solve this, we will follow these steps ? Sort the list nums Initialize p1 := 0 (left pointer) ... Read More
Sometimes we need to build a data structure that efficiently checks if any two elements sum to a target value. This problem requires designing a class with two methods: add() to insert values and find() to check pair sums. Problem Statement We need to create a data structure with two operations ? add(val) − adds the value to the data structure find(val) − checks whether there are two elements whose sum equals val The key requirement is efficiency − we should be able to answer queries quickly without searching through all numbers every time. ... Read More
Sometimes keyboard keys get stuck and produce repeated characters when typing. This program checks if a typed string could have been intended to write a target string, accounting for stuck keys that repeat characters. For example, if we want to type "apple" but the 'p' and 'e' keys are stuck, we might end up typing "appppleee". This program verifies if such typing errors are possible. Problem Statement Given two strings s (typed string) and t (target string), determine if s could be the result of typing t with some stuck keyboard keys. Example If s ... Read More
Suppose we have a list of numbers called nums. We have to check whether the list alternates starting from strictly increasing then strictly decreasing and then strictly increasing and so on. If the list is only strictly increasing, it will also be considered valid. So, if the input is like nums = [2, 4, 8, 7, 5, 1, 5, 7, 2, 1], then the output will be True, because [2,4,8] are increasing, then [7,5,1] is decreasing, then again [5,7] is increasing and [2,1] is decreasing. Algorithm To solve this, we will follow these steps ? If nums[1]
When we have a sorted array with negative and positive numbers, squaring them can disrupt the order. We need to find squared elements and return them in sorted order efficiently. So, if the input is like nums = [-8, -3, 0, 5, 6], then the output will be [0, 9, 25, 36, 64] Algorithm To solve this efficiently in O(n) time, we use a two-pointer approach ? Initialize left pointer at start (0) and right pointer at end (n-1) Create result array and fill from the end (largest squared values first) Compare absolute values at ... Read More
Sorting numbers based on the count of 1s in their binary representation is a common programming problem. When two numbers have the same count of 1s, we sort them by their actual values in ascending order. So, if the input is like nums = [4, 1, 12, 7, 6], then the output will be [1, 4, 6, 12, 7], because − Binary form of 4 is 100 (one 1) Binary form of 1 is 1 (one 1) Binary form of 6 is 110 (two 1s) Binary form of 12 is 1100 (two 1s) Binary form of 7 ... Read More
When working with arrays, we sometimes need to find pairs of elements that satisfy specific distance constraints. In this problem, we want to find the smallest sum of two elements where their indices are not consecutive (distance greater than 1). Problem Understanding Given a list of numbers, we need to find any pair of indices (i, j) where i < j and j - i > 1, then return the smallest possible sum. This means we cannot select adjacent elements. For example, with nums = [3, 4, 2, 2, 4], we can select values 3 and 2 ... Read More
Suppose we have a string s with four possible characters "1", "2", "3" and "?". We can place any one of "1", "2" and "3" in the place of "?". We have to find the smallest possible number that we can make such that no two adjacent digits are same. So, if the input is like s = "2??3?", then the output will be 21231. Algorithm To solve this, we will follow these steps − Convert the string to a list for easy modification Handle the special ... Read More
Suppose we have a list of numbers called nums. If the frequency of a most frequent number in nums is k, we have to find the length of the shortest sublist such that the frequency of its most frequent item is also k. So, if the input is like nums = [10, 20, 30, 40, 30, 10], then the output will be 3, because here the most frequent numbers are 10 and 30, where k = 2. If we select the sublist [30, 40, 30], this is the shortest sublist where 30 appears with its maximum frequency of 2. ... Read More
Suppose we have a list of numbers called nums, where n elements are present. We have to check whether we can make a list with first n natural numbers either in increasing or decreasing fashion, like [1, 2, ..., n] or [n, n - 1, ..., 1] by shifting nums to the right any number of times or not. So, if the input is like nums = [5, 6, 1, 2, 3, 4], then the output will be True, because we can shift them four times to make the array [1, 2, 3, 4, 5, 6]. Algorithm ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Economics & Finance