Algo Tech

You might also like

Download as rtf, pdf, or txt
Download as rtf, pdf, or txt
You are on page 1of 5

Array Problem:

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.

Check duplicates can make impact

Check negative number impact

Techniques:

1. Linear traversal with single pointer.

2. Linear traversal with two pointer(start/end) without sorting/with sorting

3. Using of Map/Set while traversal

4. Using partioning logic of quicksort

5. 3 ways partioning e.g detch national flag algo.

6. Sliding window approach with fixed size window.

7. Sliding window approach with variable size window.

8. Kaden's Algo

Find a pair with the given sum in an array

1.Linear traversal with single pointer

A naive solution is to consider every pair in the given array and return if the desired sum is found

2. Linear traversal with two pointer(start/end) without sorting/with sorting

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.

3. Using of Map/Set while traversal

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.

Check if a subarray with 0 /K sum exists or not

Print subarrays with 0/K sum

Print all subarrays with 0/K sum

Print longest subarray with 0/K sum

Print smallest subarray with 0/K sum

Print longest length subarray with 0/K sum

Above four question we can replace 0 with any constant number.

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

Other Solution is using hash,

In most of cases we define hashmap with <SumSoFar, Index>

While traversing we see SumSoFar - Target is present or not. and decide accordingly.

Sort binary array in linear time

Sort an array of 0’s, 1’s, and 2’s (Dutch National Flag Problem)

Move all zeros present in an array to the end

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.

Dutch National Flag Problem is similar to 2 way


We can rearrange the array in a single traversal using an alternative linear-time partition routine that
separates the values into three groups:

The values less than the pivot,

The values equal to the pivot, and

The values greater than the pivot.

To solve this particular problem, consider 1 as a pivot

Find minimum sum subarray of size k

Find maximum sum subarray of size k

Find the count of distinct elements in every subarray of size k

Above can be solved using fixed size sliding window tehnique.

Sometimes dynamic size sliding window approach can be used for some problem e.g

Find a subarray having the given sum in an integer array.

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.

Others prolems are:

Smallest subarray whose sum is > = K

One Special type of sliding window approach is kaden algo.

Maximum Sum Subarray Problem (Kadane’s Algorithm)

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,

if array consists +ve nd -ve then we reuired this approach.

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

pair in the array whose sum is equal to the given target.

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.

You might also like