Problems


January 1, 2023

Sum of all odd length subarrays

We will generate all possible subarrays of odd length and sum them. Time complexity: O(n^3) Space complexity: O(1) We can do this in one line too-

Read

January 1, 2023

Count the number of k big indices

We will use binary search twice, one from left, one from right to find out how many elements are less than the current element. Time complexity: O(nlog(n)) Space complexity:...

Read

January 1, 2023

Distinct prime factors of product of array

First we calculate all the prime numbers up to n using the Sieve of Eratosthenes. Then we iterate over the array and for each element we will increment the counter for each prime factor...

Read

January 1, 2023

Count the digits that divide a number

We will iterate over each digit and check if it is a divisor of the number. If it is, we will increment the counter. Time complexity: O(n) Space complexity: O(1)

Read

December 31, 2022

Unique paths III

We first find the start squar and end square and path length which contains all squares except the blocked. Then we start the dfs from the start node to wards the end node. And we add the node to a...

Read

December 31, 2022

Create binary tree from descriptions

We will use a dictionary to store the nodes. We will iterate over the descriptions and for each description we will create a node and store it in the dictionary. Then we will iterate over the...

Read

December 31, 2022

Sum of all subset xor totals

We will use backtracking to solve this problem. We will start from the first element and then we will add it to the current subset and then we will call the function again with the next element. Then...

Read

December 30, 2022

Remove letter to equalize frequency

We will count the frequency of each letter. Then we will find the minimum and maximum frequency. If the minimum frequency is equal to the maximum frequency, then we can remove any letter. Otherwise,...

Read

December 30, 2022

3sum smaller

First we will sort the numbers. Then we will use two pointers to find the number of triplets that sum to a smaller value than the target. Time complexity: O(n^2) Space complexity:...

Read

December 30, 2022

adding-spaces-to-a-string

We will iterate over the string and add the spaces to the result. Time complexity: O(n) Space complexity: O(n)

Read
... 10 11 12 13 14 ...