Love Babbar's Array Section
Love Babbar's Array Section
SDE Sheet
Approaches for
Array Section
With Time and Space
Complexities.
By Bhargab Sarmah
215. Kth Largest Element in an Array
Given an integer array nums and an integer k, return the kth largest element in the array. Note that it is
the kth largest element in the sorted order, not the kth distinct element.
Approach 2: 1) use a min priority queue (min heap) 2)iterate over the array and
insert in queue, after inserting check if the size of the queue is greater than k if
yes pop the top element 3) return q.top()
TC = O(nlogk) SC = O(k)
//min heap
std::priority_queue<int,std::vector<int>,greater<int>>q;
for(auto i:nums){
q.push(i);
if(q.size() >k){
q.pop();
}
}
} return q.top()
Approach 3: Using quick sort
Given an array of size N containing only 0s, 1s, and 2s; sort the array in ascending order without using sort
function.
Move all negative numbers to the beginning & positive numbers to the end
Approach:
Steps: 1) take 2 pointers i = 0, st = 0
2) run a loop while i<n
If arr[i] == –ve number swap arr[i] and arr[st] st++
TC = O(n) Sc = O(1)
189. Rotate Array
Given an array, rotate the array to the right by k steps, where k is non-negative.
Input: nums = [1,2,3,4,5,6,7], k = 3 Output: [5,6,7,1,2,3,4]
Explanation:
rotate 1 steps to the right: [7,1,2,3,4,5,6]
rotate 2 steps to the right: [6,7,1,2,3,4,5]
rotate 3 steps to the right: [5,6,7,1,2,3,4]
Approach: Steps 1) reverse the array // [ 7,6,5,4,3,2,1 ] 2) reverse the 1st k elements // [
5,6,7,4,3,2,1 ] 3) reverse the rest elements after k // [5,6,7,1,2,3,4]
TC = O(n) Sc = O(1)
Approach1: Use 2 for loops and find the largest subarray sum TC = O(n^2) Sc = O(1)
Approach: Since the elements will be from [1,n] , therefore our answer lies within that range
Steps: 1) we will use binary search and find the number of elements in the i/p arr that are <= mid st=1
en = n-1
2) if cnt is <= mid than our answer lies in the 2nd half hence, st = mid+1
3) else en = mid-1
4) return st
TC = O(nlogn) Sc = O(1)
You are given two integer arrays nums1 and nums2, sorted in non-decreasing order, and two
integers m and n, representing the number of elements
in nums1 and nums2 respectively.Merge nums1 and nums2 into a single array sorted in non-decreasing
order. Do not use extra space store the values in nums1.
The result of the merge is [1,2,2,3,5,6] with the underlined elements coming from nums1
Approach: Steps: 1) if arr in decreasing order return reverse of the arr ie. If 3,2,1 return 1,2,3
2) if not, iterate from 2nd last index and check if(arr[i] < arr[i+1]) if yes break; this means we
found a no. that can be interchanged to make next permutation. Here i = 6 (arr[i] = 7)
3) now iterate from the last index till the ith index (i we get from step 2) if(arr[j]>arr[i]) break; here j
= 9 (arr[j]=9)
4) swap arr[i] & arr[j] //1 2 3 4 2 6 9 18 12 7 0
5) reverse the arr from i+1 th index to last //1 2 3 4 2 6 9 0 7 12 18
TC = O(n) Sc = O(1)
Common elements
Given three arrays sorted in increasing order. Find the elements that are common in all three arrays.
Note: can you take care of the duplicates without using any additional Data Structure?
Input: n1 = 6; A = {1, 5, 10, 20, 40, 80} n2 = 5; B = {6, 7, 20, 80, 100}
n3 = 8; C = {3, 4, 15, 20, 30, 70, 80, 120}
Output: 20 80
n1 = 3; A = {3,3,3} n2 = 3; B = {3,3,3} n3 = 3; C = {3, 3,3} o/p = {3}
Given an array of size n and a number k, find all elements that appear more than " n/k " times.
Approach: Steps: 1) insert the frequencies of all values of nums in a map
2) Now iterate over the map:
And print those vals that are present more than n/k times
TC = O(n) Sc = O(n)
TC = O(n^2) Sc = O(1)
Approach: 1) we will try to partition both the arrays in equal halves (for odd length, left half will have 1
element more)
2) While making the partition check if max val in left half of nums1 <= min val in right half of
nums2 AND max val in left half of nums2 <= min val in right half of nums1 :
if yes
If(even length)
ans = (max(max val in left half of nums1 and nums2) + min(min values in right
halves of nums1 and nums2))/2
if odd length: ans = max(max val in left half of nums1 and nums2)
3) we will only look on the small size array (say nums1) if nums2 is small call the same function
func(nums2,nums1)
int st = 0,en = n
while(st<=en)
int cut1 = (st+en)/2;
int cut2 = (n+m+1)/2-cut1;
int left1 = cut1==0 ? INT_MIN: nums1[cut1-1];
int left2 = cut2==0 ? INT_MIN: nums2[cut2-1];
int right1 = cut1==n ? INT_MAX: nums1[cut1];
int right2 = cut2==m ? INT_MAX: nums2[cut2];
if(left1 <= right2 && left2 <= right1)
if((n+m)%2==0){
return (std::max(left1,left2)+std::min(right1,right2))/2.0;
else{
return std::max(left1,left2);
else if(left1>right2){
en = cut1-1;
else{
st = cut1+1;
TC = O(log(min(n,m)) Sc = O(1)