Professional Documents
Culture Documents
Algo Tech
Algo Tech
Algo Tech
Note:
Check for array sorted:- If not sorted, can we sort it, will it impact, like in case of finding subarray
solution we cant sort the array, as it will change the order of elements.
Techniques:
8. Kaden's Algo
A naive solution is to consider every pair in the given array and return if the desired sum is found
The idea is to sort the given array in ascending order and maintain search space by maintaining two
indices (low and high) that initially points to two endpoints of the array. Then reduce the search space
nums[low…high] at each iteration of the loop by comparing the sum of elements present at indices low
and high with the desired sum. Increment low if the sum is less than the expected sum; otherwise,
decrement high if the sum is more than the desired sum. If the pair is found, return it.
We can use a hash table to solve this problem in linear time. The idea is to insert each array element
nums[i] into a map. We also check if difference (nums[i], target - nums[i]) already exists in the map or
not. If the difference is seen before, print the pair and return. The algorithm can be implemented as
follows in C++, Java, and Python:
Note: We can find the all pairs by not stopping the execution on finding first pair.
To find out solution of above question, we can use brute force also to find all the subarray O(n^2) and
check it is meeting the expecation or not.
A simple solution is to consider all subarrays and calculate the sum of their elements. If the sum of the
subarray is equal to the given sum, print it
While traversing we see SumSoFar - Target is present or not. and decide accordingly.
Sort an array of 0’s, 1’s, and 2’s (Dutch National Flag Problem)
Above question, can be solved using the counting sort which requires extra space.
We can tweak our quick sort partioning logic and achieve the result, we will select pivot as 0/1 and in
single pass, elements will be get placed accordingly.
Sometimes dynamic size sliding window approach can be used for some problem e.g
We can solve this problem by using a sliding window. The idea is to maintain a window that starts from
the current element, and the sum of its elements is more than or equal to the given sum. If the current
window’s sum becomes less than the given sum, then the window is unstable, and we keep on adding
elements to the current window from its right till the window becomes stable again. Print the window if
its sum is equal to the given sum at any point.
Note this approach will only work with an array of positive integers.
Use the hashing technique to solve the problem with negative numbers.
Here find out max sum that can be generated, if array does not contains any negative number then
whole subarray will form the max sum, so no need to use it,
In case of all negative number the lesser negative number will be the max sum.
Note: -
2 pointers pattern
In problems where we deal with sorted arrays (or LinkedLists) and need to
find a set of elements that fulfill certain constraints, the Two Pointers
approach becomes quite useful. The set of elements could be a pair, a triplet or
even a subarray.
Given that the input array is sorted, an efficient way would be to start with one
pointer in the beginning and another pointer at the end
We can follow the Two Pointers approach. We will start with one pointer pointing to the
beginning of the array and another pointing at the end. At every step, we will see if the numbers
pointed by the two pointers add up to the target sum. If they do, we have found our pair;
otherwise, we will do one of two things:
1. If the sum of the two numbers pointed by the two pointers is greater than the target sum,
this means that we need a pair with a smaller sum. So, to try more pairs, we can
decrement the end-pointer.
2. If the sum of the two numbers pointed by the two pointers is smaller than the target sum,
this means that we need a pair with a larger sum. So, to try more pairs, we can increment
the start-pointer.
Given an array of sorted numbers, remove all duplicates from it. You should not use any extra space;
after removing the duplicates in-place return the new length of the array.
In this problem, we need to remove the duplicates in-place such that the resultant length of the array
remains sorted. As the input array is sorted, therefore, one way to do this is to shift the elements left
whenever we encounter duplicates. In other words, we will keep one pointer for iterating the array and
one pointer for placing the next non-duplicate number. So our algorithm will be to iterate the array and
whenever we see a non-duplicate number we move it next to the last non-duplicate number we’ve seen.