Problems


November 7, 2022

Number of subsequences that satisfy the given sum condition

We will sort the array and use two pointers to find the number of subsequences that satisfy the given sum condition. Time complexity: O(nlogn) Space complexity: O(1)

Read

November 7, 2022

Wiggle sort II

We will sort the array and then split it into two halves. We will iterate over the first half and the second half and insert the elements into the result array. Time complexity:...

Read

November 7, 2022

Array with elements not equal to average of neighbors

We will sort the array. Then take 2 elements in pair at a time, swap their positions and then return it after the loop is finished. Time complexity: O(nlogn) Space complexity:...

Read

November 7, 2022

Maximum 69 number

We will iterate over all the digits of the number and check if the digit is 6. If it is, we will replace it with 9 and return the number. Time complexity: O(n) Space complexity:...

Read

November 6, 2022

Orderly queue

For k>1 (we just check for k=2), any two adjacent characters can be swapped: abXYZ -> abXYZ -> aXYZb -> XYZba -> YZbaX -> ZbaXY -> baXYZ. If ab are not in the beginning of the string, i.e., DEabF, we...

Read

November 6, 2022

Jewels and stones

We will count all the distinct jewels and store it in a set. Then we will iterate over all the stones and check if the stone is a jewel. If it is, we will increment the count. We can also count...

Read

November 6, 2022

Check completeness of a binary tree

We will use BFS for level order traversal. We will add children to the BFS queue, if it's a complete tree, we should not have any node once we encounter a null node. Time complexity:...

Read

November 6, 2022

Design an ordered stream

We will use a list to store the values and a pointer to keep track of the next index to be filled. We will return the values from the pointer to the index of the current value. Time complexity:...

Read

November 6, 2022

Minimum number of vertices to reach all nodes

We can just return all nodes with on incoming edge. Necesssary condition for this to work is to have all nodes with no in-degree must in the final result, because they can not be reached from. All...

Read

November 6, 2022

Sort integers by the number of 1 bits

We will iterate over all the numbers in of the array and count the 1 bits and store it in a map. Then we will sort the map by the value and return the keys. Time complexity: O(nlogn)...

Read
... 40 41 42 43 44 ...