Suppose we have a non-empty integer array, we have to find the minimum number of moves that are required to make all array elements equal, where a move is incrementing or decrementing a selected element by 1. So when the array is like [1, 2, 3], then the output will be 2, as 1 will be incremented to 2, and 3 will be decremented to 2.To solve this, we will follow these steps −sort the array numsset counter as 0for i in nums, docounter := counter + absolute of (i – nums[length of nums / 2])return counterExample(Python)Let us see the ... Read More
Here we will see how to make a stack, that can perform push, pop, top, and retrieve the min element in constant time. So the functions will be push(x), pop(), top() and getMin()To solve this, we will follow these steps −Initialize the stack by min element as infinityFor push operation push(x)if x < min, then update min := x, push x into stackFor pop operation pop()t := top elementdelete t from stackif t is min, then min := top element of the stackFor top operation top()simply return the top elementfor getMin operation getMin()return the min elementExampleLet us see the following ... Read More
Consider we have a linked list, and we have to check whether there is any cycle or not. To represent the cycle in the given linked list, we will use one integer pointer called pos. This pos represents a position in the linked list where tail is connected. So if pos is -1, then there is no cycle present in the linked list. For example, the linked list is like [5, 3, 2, 0, -4, 7], and pos = 1. So there is a cycle, and tail is connected to the second node.To solve this, we will follow these steps ... Read More
A module is an important concept introduced in Java 9. By using this concept, we can able to divide code into smaller components called modules. Therefore, each module has its own responsibility and declare its dependency on other modules to work properly. In order to declare a module, we need to include the "module-info.java" file to root source code.There are few types of "requires" clause in "module-info" file1) requires : By default, a module doesn't know other modules present in a module-path. So, it is necessary to add a line in our module-info.java: "requires" each time when we want to access ... Read More
Suppose we have four lists A, B, C, D of integer values, we have to compute how many tuples (i, j, k, l) there are such that A[i] + B[j] + C[k] + D[l] is zero. Consider all A, B, C, D have same length of N where 0 ≤ N ≤ 500. Remember all integers are in the range of -228 to 228 - 1 and the result is guaranteed to be at most 231 - 1. So if the inputs are A = [1, 2], B = [-2, -1], C = [-1, 2], D = [0, 2], then ... Read More
Suppose we have a string, we have to sort the characters based on the frequency. So if the string is like “abbbacbcc”, then the output will be “bbbbcccaa”To solve this, we will follow these steps −create an array of pairs called v, create one map mfor all characters in string, increase value of m[character] by 1i := first element of mapwhile map has elementsinsert (i.second, i.first) into vand increase i to point to the next elementsort the vector vans := an empty stringfor i := 0 to size of vt := first element of v[i]while t is not 0ans := ... Read More
Suppose we have given a string s that consists of only uppercase letters, we can perform at most k operations on that string. In one operation, we can select any character of the string and change it to any other uppercase letters. We have to find the length of the longest sub-string containing all repeating letters we can get after performing the above operations. So if the input is like: “ABAB” and k = 2, then the output will be 4. This is because two ‘A’s with two ‘B’s or vice versa.To solve this, we will follow these steps −maxCount ... Read More
Suppose we have a non-empty string containing an out-of-order English representation of digits 0-9, output the digits in ascending order. There are some properties −Input is guaranteed to be valid and can be transformed to its original digits. That means invalid inputs such as "abc" or "zerone" are not permitted.Input length is less than 50, 000.So if the input is like “fviefuro”, then the output will be 45.To solve this, we will follow these steps −nums := an array that is holding the numbers in English letter from 0 to 9.make one array count of size 10ans := an empty ... Read More
Suppose we have a non-empty array containing only positive numbers, we have to find if the array can be partitioned into two subsets such that the sum of elements in both subsets is the same. So if the input is like [1, 5, 11, 5], the output will be true. As this array can be partitioned as [1, 5, 5] and [11]To solve this, we will follow these steps −n := size of the arraysum := 0for i := 0 to n – 1sum := sum + nums[i]if sum is odd, return falsesum := sum / 2create one array called ... Read More
Consider we have a random list of people standing in a queue. If each person is described by a pair of integers (h, k), where h is the height and k is the number of people in front of him, who have a height greater than or equal to h. We have to define one method to reconstruct the queue. So if the given array is like [[7, 0], [4, 4], [7, 1], [5, 0], [6, 1], [5, 2]], then the output will be [[5, 0], [7, 0], [5, 2], [6, 1], [4, 4], [7, 1]]To solve this, we will ... Read More