12. Arrays (easy)

int arr[n]==> garbage values if inside main

               ==> 0s if globally initialised

-------------------------------------------------------------------------------------------------------------

                           1. Remove duplicates from Sorted array:

Push all unique numbers at start and whatever at the end of uniques....

Brute
 
:(nlogn)
array se set mein daal aur fir set se array mein...

Optimal-O(n):⭐
2 pointer approach
whichever j ≠ i  ,( swap that j with i+1 ), or, ( put a[i+1]=a[j] ) 

---------------------------------------------------------------------------------------------------------------
2. Left Rotate an array by 1 place:

[1,2,3,4,5]----->[2,3,4,5,1]








----------------------------------------------------------------------------------------------------------------------------

      3. Left rotate by k-places:

Brute-              time: O(nk)

rotate by 1 place wale code ko K times loop mein chalado.

-----------------------
Better-             time: O(n+k)   space: O(k)

Using an extra helper array.

Note- if k>n we will use k%n instead of k

--------------------------------------
Optimal-    time: O(n)          space: O(1) ⭐

Learn this trick:    reverse first K elements --> reverse remaining --> reverse entire.



---------------------------------------------------------------------------------------------------------------
           4. Move all zeros at end of array-⭐

Brute     time: O(N)        space-O(N)

Using a temp array.

- put all non-zero elements into temp array & 
replace all starting elmts of og array with temp wale & later wale with 0s

--------------------------------

Optimal:   time: O(N)       space-O(1)

Using 2 pointer :
first 0 elmt pe j aur uske aage i set krke dekhte raho
if v[i] is non zero then swap it with v[j] and increment j then...

------------------------------------------------------------------------------------------------------------

6. Union of two Sorted arrays containing duplicates

Note- we need elements in union in ascending order + we want all the common and distinct elements. 
----------------
Brute

Store elements from both arrays in an ordered set.




 

-------------------------------------------------------

Optimal- O(n1+n2)

using 2 pointer
we just modify the if conditions to work for duplicates also

-------------------------------------------------------------------------------------------------------------

6. Intersection of two Sorted Arrays containing duplicates

Brute-  O(n1*n2),                     space- O(n2)

For each element in v1 scan entire v2 and then store only common elements in both.
But maintain a visited array for v2, so we can handle duplicates.

------------------------------------------------------------

Optimal-    time: O(n1+n2)                     space: O(1)

2 pointer

                                  
--------------------------------------------------------------------------------------------------------------                            7. Find Missing number in array from 1 to n:

1. brute- (time: n^2   & space:1)     use 2 nested loops and perform searching
2. better- (time:n      &  space:n)     freq map
3. optimal- (time:n   &  space:1)     use sum of first n numbers formula 
                  or, bit manipulation

------------------------------------------------------------------------------------------------------------
8. Count max consecutive 1s in a binary array-

Optimal: single pass using a counter

------------------------------------------------------
Even a similar approach with a
2 pointer can also be used :
---------------------------------------------------------------------------------------------------------------
9. Find no. that appears once if, others appear twice-

Brute- time: O(n^2)

using linear search
for every element, we search corresponding pair in array & whichever element occurs only once in the array is our answer.
----------------------------------------------

Better-  time: O(n)      space:O(n)

using mapping
we prefer unordered map

------------------------------------------------
Optimal-  time: O(n)   space: O(1)
using XOR property
--------------------------------------------------------------------------------------------------------------
  10. Longest subarray with sum k:⭐

Brute-   time: O(n^2)        space: O(1)
use 2 nested loops
---------------------------------------------------------------------------------

Better:  time- O(nlogn)     space-O(n) 

Using mapping & prefix sum
(optimal for array containing positives & negatives)
Prefer unordered map

1. we store in a map, index vs sum of elements till that index
2. then we iterate on the array and if at an index sum is X and there exists a sum of X-k before that, then we can say we found a subarray with sum K

Below code is for single pass, we can instead do it in two pass also, where firstly we store all sums as key and indices as values & in second pass we perform searching for x-k sum






For longest subarray, we prefer x-k as left as possible, So we 
update the map with the sum only if that sum was not present in the map.

---------------------------------------

Optimal-  time: O(n)   space: O(1)

Sliding window approach
Optimal for array containing only positives

----------------------------------------------------------------------------------------------------------

Comments

Popular posts from this blog

30.) DP on subsequences

19. Strings (LB)

32. Binary Trees-2 (bfs/dfs med problems)