Here are this week's questions
1) Find All Duplicates in an Array
Hash Map
While traversing the array, check if the current element is present in the map or not, if its present then thats a duplicate element, push it into the ans array and continue. If not then store it into map.
One Pass & Constant Space
The idea is to use the elements as indexes and mark the positions reached by them as negative of their original value. By doing so, we determine that if we reach that position again(i.e it will be negative), then that means the element which we used as an index was a duplicate number.
Perform a DFS traversal and for each traversal count the no. of ones and mark the visited position zero. Then return the maximum obtained by all areas.
Basic reversing a integer, just check whether it exists in integer range or not.
4) Add Binary
Perform the binary addition on strings by maintaining the carry.
Perform the reverse operation and compare.
6) Happy Number
Perform the necessary logic.
Actual sum - array sum, will give missing number.
8) Maximum Product of Three Numbers
After sorting, last 3 digits product or first 2 digit and last digit product.
9) Power of Two
Use right shift operator(>>) to shift the number and count the ones present in it binary form. If count is equal to 1 then its a power of two.
10) Minimum Moves to Equal Array Elements
Increasing or decreasing of the elements is same. So, the idea is to decrease all the elements to zero. We can achieve this by first subtracting the elements by minimum number and then adding the sum of remaining elements.
array = [2, 3, 4]
subtract 2 (minimum no.) from all the elements
array = [0, 1, 2]
now to make the array all zero, no. of operations
will be equal to sum of all the elements.
formula generated = sum of array - minNum * size of array
11) Minimum Deletions to Make Array Divisible
The idea is simple, a number can divide a group of numbers when the gcd of all those numbers is divisible by that number. So, we calculate the gcd of all those numbers and then for removing the smallest number who does not divide we sort the given array and then we just check.
Top comments (0)