Problems


December 1, 2022

Majority element II

We will count the frequency of each element in the array and return the elements whose frequency is greater than n//3. We will use a hashmap to store the frequency of each element, and a...

Read

December 1, 2022

Path sum IV

We will convert the given array into a tree using Node objects. Afterwards, for each path from root to leaf, we can add the sum of that path to our answer. There are two steps, the tree...

Read

December 1, 2022

Single row keyboard

We will iterate over the word, find the index of the character in the keyboard and add the difference between the current and previous index to the result. Time complexity: O(n)...

Read

November 30, 2022

Bitwise xor of all pairings

From Boolean algebra we know that n^0 = n and n^n = 0, from which we can infer that the xor of an even count of an integer n is 0, and the xor of an odd count of an integer n is n. We also know that...

Read

November 30, 2022

Count nodes equal to average of subtree

We will traverse the tree in post-order fashion. For each node, we will calculate the sum of all nodes in its subtree and the number of nodes in its subtree. Then we will check if the average of the...

Read

November 30, 2022

Find all lonely numbers in the array

We will count the occurrences of each number in the array and return the numbers that have only one occurrence and no adjacent number is present in the list. Time complexity: O(n), n...

Read

November 30, 2022

Find leaves of binary tree

We will traverse the tree in post-order fashion. For each node, we will check if it is a leaf node. If it is, we will append the value of the node in a list. Then we will check if the left and right...

Read

November 30, 2022

Fizz buzz

We will iterate over the numbers from 1 to n and append the corresponding string to the result list. If the number is divisible by 3, we will append "Fizz" to the result list. If the number is...

Read

November 30, 2022

Maximum average subtree

We will traverse the tree in post-order fashion. For each node, we will calculate the sum of all nodes in its subtree and the number of nodes in its subtree. Then we will check if the average of the...

Read

November 30, 2022

Maximum split of positive even integers

We can be greedy and try to split the number into as many parts as possible. We will start from the 2 and try to split it until we reach the end of the number. If we can't split the number, we will...

Read
... 25 26 27 28 29 ...