0% found this document useful (0 votes)
2K views7 pages

Striver A2Z DSA Sheet: Array Problems

The document provides revision notes for the Striver A2Z DSA Sheet, detailing various approaches to solve common array problems. It covers topics such as finding the largest and second largest elements, checking if an array is sorted, removing duplicates, rotating arrays, moving zeros, linear search, union of arrays, finding missing numbers, and counting consecutive ones. Each problem includes multiple approaches with their respective time and space complexities.

Uploaded by

n200677
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2K views7 pages

Striver A2Z DSA Sheet: Array Problems

The document provides revision notes for the Striver A2Z DSA Sheet, detailing various approaches to solve common array problems. It covers topics such as finding the largest and second largest elements, checking if an array is sorted, removing duplicates, rotating arrays, moving zeros, linear search, union of arrays, finding missing numbers, and counting consecutive ones. Each problem includes multiple approaches with their respective time and space complexities.

Uploaded by

n200677
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Revision Notes for Striver A2Z DSA Sheet

Sheet :- [Link]

ARRAYS - EASY

1. Largest Element in Array

Approach 1 :- Sort array ascending, print last ele. TC:- O(N*log(N)) SC: O(n)

Approach 2 :- Use max variable TC:- O(n) SC:- O(1)

2. Second Largest Element in Array


Approach 1 :- Sort it, print second last TC:- O(NlogN) SC:- O(1)
Approach 2 :- Find max ele in one traversal, then find the number just smaller in next
traversal TC :- O(2N) SC:- O(1)

Approach 3 :- Traverse array, If the current element is larger than ‘large’ then update
second_large and large variables Else if the current element is larger than ‘second_large’ then
we update the variable second_large. Once we traverse the entire array, we would find the
second largest element in the variable second_large.

Tc :- O(N) Sc:- O(1)

3. Check if array is sorted


Approach 1 :- Take elements one by one then traverse the array completely in front of it. TC
:- O(n2)
Approach 2 :- Check if previous element is smaller than previous one, if false then it is not
sorted. TC :- O(N) SC:- O(1)

VANSHIKA MISHRA 1
Revision Notes for Striver A2Z DSA Sheet

4. Remove duplicated in place from sorted array


Approach 1 :- Declare Hashset, put array elements in set, put all elements of set to a new
array TC:-O(NlogN)+O(n) SC:-O(n)

Approach 2 :- Two Pointers – have two pointers i and j . check if arr[i]!=arr[j] then arr[i] will
be arr[j], basically iterate over same elements using j without doing anything , when find
unique elements swap with element at i. TC :- O(N) SC:- O(1)

5. Right Rotate Array by k places (includes 1 place)


Approach 1 :- Copy k elements to temporary array, shift elements to empty positions now.
Put k elements in the front again from the temp array. TC :- O(n) SC:- O(k)
Approach 2 :- Reverse last k ele and (n-k) ele, reverse whole array now TC:- O(N) SC:- O(1)

VANSHIKA MISHRA 2
Revision Notes for Striver A2Z DSA Sheet

6. Move all zeros to the end of the array


Approach 1 :- Make temp array, copy all non-zero elements to it then fill the rest with zeroes.
Tc :- O(2N) SC:-O(N)
Approach 2 :- Use 2 pointers , move i when encounter non-zero element, move j after swap.
In case of non zero swap element at i and j else keep moving i forward. TC :- O(N) SC:- O(1)

7. Linear Search
Approach 1: Search element in array, return the index. TC:- O(n) SC:- O(1)

VANSHIKA MISHRA 3
Revision Notes for Striver A2Z DSA Sheet

8. Union of two arrays


Approach 1 :- Use Hashset, put all elements in it to find distinct elements. TC:-
O(n+m(log(n+m)) SC:- O(n+m)

In case that the two given arrays are sorted


Approach 1 :- Two Pointers approach, create a union arraylist, move one pointer in each
array in case :- if a[i]==b[j] add a[i], if a[i]<b[j] add a[i] first then i++, ], if b[j]<a[i] add b[j] first
then j++. TC:-O(n+m) SC:-O(n+m)

VANSHIKA MISHRA 4
Revision Notes for Striver A2Z DSA Sheet

9. Find the missing number in an array


Approach 1 :- Do linear search for each ele in [0..n]. any ele not found is ans. TC:-O(N2) SC:-
O(1)
Approach 2:- Put ele [0…n] in hash array then count frequency, the one with zero freq is ans.
TC:- O(2N) SC:-O(N)
Approach 3:- Calculate the sum of natural nos [0..n] using formula, calulate summation of
array, subtract and find which one is missing TC:- O(N) SC:-O(1)

Approach 4:- XOR

VANSHIKA MISHRA 5
Revision Notes for Striver A2Z DSA Sheet

10. Find maximum consecutive ones


Approach :- keep count variable keep doing count++ till we find 1 and update maxcount with
every iteration of counting ones when hit 0, reset count to 0. TC:- O(N) SC:-O(1)

11. Subarray with Sum K


Approach 1 :- Generate all subarrays then check how many have the desired sum K.

VANSHIKA MISHRA 6
Revision Notes for Striver A2Z DSA Sheet

Approach 2 :- Save Prefix Sums in Hash Map. Check if the required prefix sum is available to
make the desired sum. If yes increase the count (same can be for length if yes then check if
it’s the maximum length). Put the updated sum in map.
TC :- O(nlogn) Sc:- O(n)

VANSHIKA MISHRA 7

You might also like