Problems
December 27, 2022
Next palindrome using same digits
We will find the next permutation that is greater than the first half of the palindrome, say greaterHalf. Then add it to the mid item if there is one and to reversed of greaterHalf. Time...
ReadDecember 27, 2022
Perfect number
We will find all the divisors of the number and then sum them up. If the sum is equal to the number, then it is a perfect number.
Time complexity: O(sqrt(n))
Space complexity:...
December 27, 2022
Prefix and suffix search
We will generate all possible prefixes and suffixes for each word and store them in a hashmap. Then, we will iterate over the queries and return the maximum weight. Time complexity:...
ReadDecember 27, 2022
Synonymous sentences
We will create a graph where the nodes are the words and the edges are the synonyms. Then we will do a DFS to find all the possible sentences and add that to a hashset to remove duplicates. Finally,...
ReadDecember 27, 2022
XOR operation in an array
We will generate the array and then XOR all the elements.
Time complexity: O(n)
Space complexity: O(1)
We can also use reduce
function to XOR all the...
December 26, 2022
Bitwise or of all subsequence sums
We will iterate over the array and update the prefix sum and bitwise or of all subsequence sums.
Time complexity: O(n)
Space complexity: O(1)
December 26, 2022
Out of boundary paths
We will solve the problem recursively. We will use a memo to store the number of paths from (i, j)
to (m, n)
.
Time complexity: O(mn)
Space complexity:...
December 26, 2022
Shortest unsorted continuous subarray
We will sort the array and find the first and last index where the elements are different.
Time complexity: O(nlog(n))
Space complexity: O(n)
December 25, 2022
Count anagrams
It is obvious that the answer is the product of the number of unique permutations for each word in a sentence. The last one is just the number of permutations of all letters (treating same letters as...
ReadDecember 25, 2022
Longest subsequence with limited sum
We will sort the numbers and then create a prefix sum array. Then we will iterate over the numbers and find the index of the largest number that is less than or equal to limit - nums[i]
....