Suppose we have a string s with lowercase letters. We have to find all in s where there must be another substring in s at a different location that is an anagram of that taken substrings. We have to find a list of substrings in in lexicographic order.So, if the input is like s = "abcba", then the output will be ['a', 'a', 'ab', 'abc', 'abcb', 'b', 'b', 'ba', 'bc', 'bcba', 'cb', 'cba'] for each of them we can find different anagrams present in the string itself.To solve this, we will follow these steps −res := a new listL := ... Read More
Suppose we have a number say n. We have to check whether n is present in Fibonacci sequence or not. As we know in Fibonacci sequence f(i) = f(i-1) + f(i-2) for each i from 2 to n, and f(0) = 0, f(1) = 1.So, if the input is like n = 13, then the output will be True, as some terms in Fibonacci sequence are: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, so 34 is present.To solve this, we will follow these steps −phi := 0.5 + 0.5 * square root of(5.0)a := phi * nreturn ... Read More
Suppose we have a list of elements called nums where all items are unique, and they are sorted in ascending order, we have to find the minimum i such that nums[i] = i. If we cannot find any solution, then return -1. We have to solve this problem in O(log(n)) time.So, if the input is like nums = [-4, -1, 2, 3, 8], then the output will be 2, because both nums[2] = 2 and nums[3] = 3 but 2 is smaller.To solve this, we will follow these steps −ret := -1, lhs := 0, rhs := size of nums ... Read More
Suppose we have a list of numbers called rooms and another target value t. We have to find the first value in rooms whose value is at least t. If there is no such room, return -1.So, if the input is like rooms = [20, 15, 35, 55, 30] t = 30, then the output will be 35. Because 30 is smaller than 35 and previous rooms are not sufficient for target 30.To solve this, we will follow these steps −for each room in rooms, doif room >= t, thenreturn roomreturn -1ExampleLet us see the following implementation to get better ... Read More
Suppose we have a list of numbers called nums whose length is at least 2. We have to find the index of every peak in the list. The list is sorted in ascending order. An index i is a peak when −nums[i] > nums[i + 1] when i = 0nums[i] > nums[i - 1] when i = n - 1nums[i - 1] < nums[i] > nums[i + 1] elseSo, if the input is like nums = [5, 6, 7, 6, 9], then the output will be [2, 4], as the element at index 2 is 7 which is larger than ... Read More
Suppose we have a number n. We have to find the least positive value x such that x is made up of only two digits 9's and 0's, and x is multiple of n.So, if the input is like n = 26, then the output will be 90090.To solve this, we will follow these steps −m := 9x := 1while m is not divisible by n, dox := x + 1m := replace all 1s with 9s in the binary form of xreturn m as integerExampleLet us see the following implementation to get better understanding −def solve(n): m = ... Read More
Suppose we have a list of elements called nums, we have to check whether all numbers appear even times or not. We have to solve it using constant space.So, if the input is like nums = [8, 9, 9, 8, 5, 5], then the output will be True, because all number have occurred twice.To solve this, we will follow these steps −if size of nums is odd, thenreturn Falsesort the list numsfor i in range 1 to size of nums, doif nums[i] is same as nums[i - 1], thennums[i] := 0, nums[i - 1] := 0return true when sum of ... Read More
Suppose we have a list of numbers called nums, we have to check whether there is an element whose frequency is same as its value.So, if the input is like nums = [2, 5, 7, 5, 3, 5, 3, 5, 9, 9, 5], then the output will be True, because 5 appears 5 times.To solve this, we will follow these steps −nums_c := a list containing frequencies of each elements present in numsfor each value i and frequency j in nums_c, doif i is same as j, thenreturn Truereturn FalseExampleLet us see the following implementation to get better understandingfrom collections ... Read More
Suppose we have a list of numbers called nums. We have to find the number of pairs i < j such that nums[i] and nums[j] are same.So, if the input is like nums = [5, 4, 5, 4, 4], then the output will be 4, as We have index pairs like (0, 2), (1, 3), (1, 4) and (3, 4).To solve this, we will follow these steps −c := a list containing frequencies of each elements present in numscount := 0for each n in list of all values of c, docount := count + floor of (n *(n - 1)) ... Read More
Suppose we have a set of Cartesian points in a list called points. We have to sort them based on their polar angles. The polar angles vary in range 0 and 2*PI. If some points have same polar angles, then arrange them based on the distance of that point from the origin.So, if the input is like points = [(1, 1), (1, -2), (-2, 2), (5, 4), (4, 5), (2, 3), (-3, 4)], then the output will be [(5, 4), (1, 1), (4, 5), (2, 3), (-3, 4), (-2, 2), (1, -2)]To solve this, we will follow these steps −Define ... Read More
 Data Structure
 Networking
 RDBMS
 Operating System
 Java
 iOS
 HTML
 CSS
 Android
 Python
 C Programming
 C++
 C#
 MongoDB
 MySQL
 Javascript
 PHP