# Problems

May 17, 2023

## Number of good leaf nodes pairs

We can traverse the tree and find out the distance between each pair of leaf nodes. If the distance is less than or equal to the given distance, we increment the result by 1. Time complexity:...

ReadMay 17, 2023

## Diameter of N-ary tree

We will use recursive DFS to find out the longest path in the tree. The longest path will be the diameter of the tree.
Time complexity: `O(n)`

where n is the number of nodes in the...

May 15, 2023

## Remove sub folders from the filesystem

We can use a trie data structure to store all the folders. We will iterate over the folders and insert them into the trie. If we find a folder that is already present in the trie, we will mark it as...

ReadMay 14, 2023

## Consecutive characters

We will keep track of the consecutive characters in a variable `count`

. We will iterate over the string and if the current character is same as the previous character, we will increment...

May 13, 2023

## Maximum product of two elements in an array

We will use a heap to get the two largest elements in the array. Then we will return the product of the two largest elements each substructed by 1.
Time complexity: `O(n)`

Space...

May 13, 2023

## Count triplets that can form two arrays of equal xor

We will calculate the prefix xor for each index. Then we will iterate over all possible pairs of indices and check if the xor of the prefix xors of the two indices is 0. If it is, we will increment...

ReadMay 13, 2023

## Make array zero by subtracting equal amounts

We can solve this problem by using a heap. We will add all the elements to the heap. Then we will pop the largest element from the heap and substruct it from all the other elements. We will repeat...

ReadNovember 1, 2022

## Letter tile possibilities

We will use DFS to generate all possible strings. We will generate all the strings, put it in a set for remove duplication and finally return the count as the result. Time complexity:...

ReadMay 12, 2023

## Android unlock patterns

We can use backtracking to solve the problem. We will start from the first number and try to find all possible patterns. We will use a hashmap to store the numbers that are already used. Now we will...

ReadMay 12, 2023

## Count ways to build good strings

All we care about is low and high. We subtruct from high number of zeros or number of ones if can. Same we do for low but max it with 0. If we reached 0 in low than we add one to the answer. Time...

Read