Problems


September 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...

Read

September 1, 2022

Minimum height trees

We will first create an adjacency list from the edge list. Then we start BFS from the leaf nodes, and whenever we visit a node, we add this to a list for current leaves, and decreasing the node count...

Read

September 1, 2022

Clumsy factorial

If we calculate some values, then we will notice some pattern. Here are some calculations- From this we can create the following method to calculate the clumsy factorial. Time Complexity:...

Read

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))...

Read

August 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....

Read

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....

Read

August 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:...

Read

August 30, 2022

Remove duplicates from sorted list II

We will have 2 pointers, when we iterate over each node, we check if we found duplicate, then we move right pointer until we hit another value. Then remove all nodes between 2 pointers and move both...

Read

August 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...

Read

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
... 68 69 70 71 72 ...