Array

Array

Element in array is identified by index. The size is fixed => when array is full not easy to add an element.

Common Array algorithms

  • Using Multi-Dimensional Arrays Multi-dimensional arrays are a common way to add complexity to array problems in an interview. It’s critical that you be comfortable applying all the common array operations in multiple dimensions

  • Using Multiple Pointers While not exactly a singular algorithm, traversing an array with multiple pointers simultaneously is a very important technique

Merge arrays

Given 2 sorted arrays, A and B, where A is long enough to hold the contents of A and B, write a function to copy the contents of B into A without using any buffer or additional memory.

Solution

We are traversing starting from the tail of A array comparing and choosing the largest value among A tail and B tail.

Important is the second while loop, which makes sure that all values of B are ending up in A.

Find duplicates in array
  • Sliding Windows This is a super useful technique used for finding subarrays that match specific criteria. It also demonstrates a lot of other relevant techniques such as using multiple pointers in an array

Sliding window (find max sum of subarray of size k)

The use of the Sliding Window technique can be done in a very specific scenario, where the size of the window for computation is fixed throughout the complete nested loop. Only then the time complexity can be reduced.

How to use Sliding Window Technique?

The general use of the Sliding window technique can be demonstrated as follows:

  1. Find the size of the window required

  2. Compute the result for 1st window, i.e. from the start of the data structure

  3. Then use a loop to slide the window by 1, and keep computing the result window by window.

How to Know, Where we use the Sliding Window?

To know, Where we use the Sliding Window then we remember the following terms which is mentioned below:

Array, String, Sub Array, Sub String, Largest Sum, Maximum Sum, Minimum Sum

Examples to illustrate the use of the Sliding window technique

Example: Given an array of integers of size β€˜n’, Our aim is to calculate the maximum sum of β€˜k’ consecutive elements in the array.

Input : arr[] = {100, 200, 300, 400}, k = 2 Output : 700

Input : arr[] = {1, 4, 2, 10, 23, 3, 1, 0, 20}, k = 4 Output : 39 We get maximum sum by adding subarray {4, 2, 10, 23} of size 4.

Input : arr[] = {2, 3}, k = 3 Output : Invalid There is no subarray of size 3 as size of whole array is 2.

Naive Approach: So, let’s analyze the problem with Brute Force Approach. We start with the first index and sum till the kth element. We do it for all possible consecutive blocks or groups of k elements. This method requires a nested for loop, the outer for loop starts with the starting element of the block of k elements, and the inner or the nested loop will add up till the kth element.

Time complexity: O(k*n) as it contains two nested loops. Auxiliary Space: O(1)

Sliding Window Technique: The technique can be best understood with the window pane in the bus, considering a window of length n and the pane which is fixed in it of length k. Consider, initially the pane is at extreme left i.e., at 0 units from the left. Now, co-relate the window with array arr[] of size n and pane with current_sum of size k elements. Now, if we apply force on the window, it moves a unit distance ahead. The pane will cover the next k consecutive elements.

Applying the sliding window technique :

  1. We compute the sum of the first k elements out of n terms using a linear loop and store the sum in variable window_sum.

  2. Then we will graze linearly over the array till it reaches the end and simultaneously keep track of the maximum sum.

  3. To get the current sum of a block of k elements just subtract the first element from the previous block and add the last element of the current block.

https://youtu.be/9-3BXsfrpbY?list=PLqM7alHXFySEQDk2MDfbwEdjd2svVJH9p

https://practice.geeksforgeeks.org/explore?page=2&category[]=sliding-window&sortBy=submissions

Sliding window (find subarray with sum of elements resulting to S)

Given an unsorted array A of size N that contains only positive integers, find a continuous sub-array that adds to a given number S and return the left and right index(1-based indexing) of that subarray.

In case of multiple subarrays, return the subarray indexes which come first on moving from left to right.

Note:- You have to return an ArrayList consisting of two elements left and right. In case no such subarray exists return an array consisting of element -1.

Example 1:

Example 2:

Video description of the solution

  • Sorting and Searching Arrays are a common format for data that we want to sort and search. Knowing how to do binary search as well as sort using common algorithms like Mergesort and Quicksort is a must

Find longest consecutive array

Multi dimentional arrays

Other challenges

Three sum

Last updated

Was this helpful?