Problems
September 1, 2022
Insertion sort list
We will create a dummy node and attach it at the beginning to make our life easier. Then we take 2 pointer, previous and current, previous will be our head and current will be the next node of...
ReadSeptember 1, 2022
Next permutation
We will take a pointer at the end of the list and move towards front until we a bigger value than the previous. If we reach all the way till the beginning, the the list is already in decreasing...
ReadSeptember 1, 2022
Bitwise and of numbers range
If the right crosses the next 2^n of left where n is the number of bits for left, that means 2^n >= left, then the result is always going to be 0. For example, AND product of 1010
and...
August 31, 2022
Rearrange words in a sentence
We will split the word with space as delimeter, sort them according to their length, join then again with space, finally return the capitalize string.
Time Complexity: O(nlog(n))
...
August 31, 2022
Continuous subarray sum
We will take a hashmap and keep the reminder along with their index in it. As a base case, we will take the subarray sum as 0 and keep the index -1 as we don't added anything yet in the subarray sum....
ReadAugust 31, 2022
Subarray sum equals k
This problem can't be solved using sliding window as it is not only positive, or only negative or sorted. So, we iterate over each item in the input, calculate prefix sum and store it in a hashmap....
ReadAugust 30, 2022
Get equal substrings within budget
We will take 2 pointer, we will forward our right pointer, added the difference of s and t in a running sum and until it is less than max cost, we forward the right pointer. Then we forward the left...
ReadAugust 30, 2022
Maximum xor of two numbers in an array
we will create a mask of first character 1 for all 32 position in a 32 bit integer. Then we created hashset to calculate which number has the largest most significant bit. Then we create a temporary...
ReadAugust 30, 2022
Remove duplicates from sorted list
We will check the current value to the next node value, if they are equal, we remove the current one, and move on till the end of the list.
Time Complexity: O(n)
Space Complexity:...
August 30, 2022
Minimum moves to make array complementary
First we create an overlay lookup hashmap of size 2limit+2, as our minimum boundary of change is 2 and maximum is 2limit. Then we iterate over the elements in pair, take the first and last element,...
Read