12. arrays easy

 Array contains elements with similar data types

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

               ==> 0s if globally.

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

  1. rotate✅
  2.  intersection🔥
  3. all zeros to end✅
  4. missing number✅
  5. number appears once✅
  6. longest subarray with sum k🔥
    1. Largest, 2nd largest✅
    2. remove duplicates✅
    3. union✅
    4. max consecutive 1s✅
    1. sorted check✅

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

                                                  LARGEST

    Ans- (for positive elements)

    BRUTE(nlogn)- sort it and then do as asked

    OPTIMAL(n)-use loops

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

                                              2nd LARGEST

    Ans-(for positive elements)

    BRUTE- sort wala with duplicate in consideration

    BETTER- 2 loops with    ,,            ,,     ,,

    OPTIMAL-single loop with 2 if conditions

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

    Take 2nd largest as -1(all +ve elements) or, INT_MIN(-ve also)

    Take 2nd smallest as INT_MAX;

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

                                     Check array sorted or not

    check if, ( nxt elmt >= prvs ) or not

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

                                Remove duplicates from array (in place) 

    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] ) 

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

    Left Rotate by one place-

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








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

    Left rotate by k-places:

    BRUTE -

    temp array mein first k elmts daaldo 

    baakiyo ki shifting krdo piche

    unke aage temp wale bhardo.


    BETTER- O(n+k),       space-O(k)
    if k>n we will use k%n instead of k.

    OPTIMAL-    TRICK    O(2N),          space-O(1)

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

                                     Right Rotate By k-places

    BRUTEO(kn)  or u can also do in n+k approach












    -----------------------------------------------------------------------------------
    Move all zeros at end of array-

    1. BRUTE time-O(N),        space-O(N)

    put all non-0 elmts into temp array
    replace all starting elmts of array with temp wale & later wale as 0s.


    2. OPTIMAL -   
    time- O(N),                space-O(1)
    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...

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

    Union of 2 sorted arrays-

    do not repeat any element

    1. Brute-




    using sets

    for unsorted arrays also

     

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

    2. Optimal- O(n1+n2)
    using 2 pointer
    only for sorted arrays
    space to return-O(n1+n2)
    no extra space
















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

    Intersection of 2 Sorted Arrays-

    BRUTE-  O(n1*n2),                     space- O(n2)
    (n2 is smaller array)










    OPTIMALO(n1+n2)                     space-O(1)

    (2 pointer)















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

    1. brute- searching (use 2 nested loops)
    2. better- freq map
    3. optimal- use sum of first n numbers formula 
                      or, bit manipulation

    ---------------------------------------------------------------------------------
    Count max consecutive 1s which appear in an array-
    optimal:
    ---------------------------------------------------------------------------------------------------------------
    Find no. that appears once if, others appear twice-

    1.BRUTE- linear search with O(n^2)
    ---------------------------------------------
    2.BETTER

    small array size- hashing
    large array size- mapping- map<long long,int>
    time complexity-   M+NlogM
    space complexity- M
    where,                   M = 1+ (n/2)
    ----------------------------------------------------
    3. OPTIMAL
    -------------------------------------------------------------
       Longest subarray with sum k-

    BRUTE use 2 nested loops


                







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

    BETTER:     time- O(nlogn) for ordered map,  space-O(N)


     

                                                           
















    Edge case-











    so we want x-k as left as possible, for longest subarray

    so better code for both (0s & +ves)    OR      optimal for +ve, -ve, 0s --> 

    just update the map with the sum only if that sum was not there in the map prvsly...

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

    OPTIMAL-  time- O(2N),   space-O(1)

    2 pointer approach

    for positives only

















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

    Comments

    Popular posts from this blog

    18. greedy algorithm

    19. strings (LB)

    17. BINARY SEARCH on 2d arrrays