Rotate Array by K places | Union, Intersection of Sorted Arrays | Move Zeros to End | Arrays Part-2

Поділитися
Вставка
  • Опубліковано 2 жов 2024

КОМЕНТАРІ • 923

  • @takeUforward
    @takeUforward  Рік тому +222

    Let's march ahead, and create an unmatchable DSA course! ❤
    Timestamps someone please :)
    Use the problem links in the description.

    • @brajeshmohanty2558
      @brajeshmohanty2558 Рік тому +7

      At 23:47 my mind just got blown away. Me be like: - itna easy kese Benstocks 😂🙏. Bhai iPad ka notes ka bhi pdf dalo na . Tumaaher writing sei sab samajh mai ata hai. Boh tuf sight par itna bada article dekhne sei dar lagta hai 🙂. But you are doing amazing job bhai thank you 🙏😌.

    • @Atharva_kathiyawadi
      @Atharva_kathiyawadi Рік тому

      appreciable !!

    • @ParasSharma-mf8or
      @ParasSharma-mf8or Рік тому

      Please upload the next video.

    • @gautamsethiya1397
      @gautamsethiya1397 Рік тому

      @takeUforward
      class Solution {
      public:
      void rotate(vector& nums, int k) {
      int n = nums.size();

      k =k%n;
      int temp[k];
      for (int i = n - k; i < n; i++)
      {
      temp[i - n + k] = nums[i];
      }
      for(int i=n-k-1;i>=0;i--)
      {
      nums[i+k]=nums[i];
      }
      for(int i=0;i

    • @yaswanthmitta8983
      @yaswanthmitta8983 Рік тому

      for moving zeroes to the end we cen write it in 1 loop instead of two though time complexity is same , complexity for understanding decreases : v oid moveZeroes(vector& nums) {
      int j=0;
      for(int i=0;i

  • @zenithria_414
    @zenithria_414 3 місяці тому +118

    Who is grinding their head with DSA in their summer vacation?? Heartly thanks to striver for making us understand dsa in such depth :)

    • @prasannagodiyal6736
      @prasannagodiyal6736 2 місяці тому

      Me😃

    • @avgguy7129
      @avgguy7129 2 місяці тому

      me too lol

    • @JohnCena-uf8sz
      @JohnCena-uf8sz 2 місяці тому

      bro this course from his website is completely free for now right ? ... or it has premium paid version as well ?

    • @victory7361
      @victory7361 2 місяці тому

      What do u mean by summer vacation 😢?

  • @sahadevbhaganagare4761
    @sahadevbhaganagare4761 Рік тому +445

    me first getting the optimal solution and then finding the brute 🤣🤣

    • @he_KARTIK
      @he_KARTIK 9 місяців тому +8

      mee too😂

    • @Vikas-dm9uc
      @Vikas-dm9uc 8 місяців тому +6

      #me2 moment

    • @helloworld-n1z
      @helloworld-n1z 7 місяців тому +6

      how to remember logic ,i always forget nd cant able to code

    • @sahadevbhaganagare4761
      @sahadevbhaganagare4761 7 місяців тому +40

      @@helloworld-n1z I am not the right person but still always go back to the code after a week or so and try solving it again. Then review it after a month. That is it, it will get hard coded in your brain after that.

    • @Me_Meraj
      @Me_Meraj 7 місяців тому +4

      Bro me too 😢
      Isi wajah se easy problem solve krne me v time lgta h 😂😂 dimag me bs chlta h ki optimal hi krna h

  • @bishalkundu7592
    @bishalkundu7592 Рік тому +126

    Thanks a lot sir for taking out time from your busy schedule and making videos and uploading those videos in the span of 2-3 days. It takes a lot ot effort. Thanks a lot 😊😊

  • @AniketKumar-tc8cz
    @AniketKumar-tc8cz Рік тому +145

    Timestamps:
    0:00:00 - Intro
    0:00:55 - Q1. Left rotate an array by 1 place
    0:07:33 - Q2. Left rotate an array by D places
    0:26:36 - Follow up Right rotate an array by D places
    0:27:11 - Q3. Moving Zeroes to end
    0:40:54 - Q4. Linear Search
    0:43:01 - Q5. Union of Two Sorted Arrays
    0:59:04 - Q6. Intersection of Two Sorted Arrays
    1:12:22 - Outro

  • @AdityaKumar-gx2ip
    @AdityaKumar-gx2ip Рік тому +60

    For Right Rotation of Array
    void rotate(vector arr, int d)
    {
    if(d>size)
    d = d % size;
    reverse(arr.begin(),arr.end());
    reverse(arr.begin()+d,arr.end());
    reverse(arr.begin(),arr.begin()+d);
    }

  • @valiant4498
    @valiant4498 8 місяців тому +13

    Guys i just wanna ask something....r u guys doing all these problems on your own or like just watching the lectures

    • @ishanmoykhede9484
      @ishanmoykhede9484 8 місяців тому +10

      First Understanding the problem then finding approach to solve it then if I make the correct solution then codding it in vs code and moving to nxt question and if I didn't get the solution then watching striver

  • @luckshaeey
    @luckshaeey Рік тому +87

    27:00 rotating an array left by 1 place is equal to rotating it right by n-1 places. We can use this information for rotating an array to right by d places.

    • @takeUforward
      @takeUforward  Рік тому +54

      Perfect thought!

    • @bhargavanand7325
      @bhargavanand7325 Рік тому

      great

    • @himanshukaushik9223
      @himanshukaushik9223 Рік тому +2

      Bhai leetcode pa left rotation question reverse .end() and begin() use karna pad Raha hai but gfg ma nahi why

    • @bhargavanand7325
      @bhargavanand7325 Рік тому +4

      @@himanshukaushik9223 in case of vector we use stl function like begin() and end() but in case of array we don't use it for example in gfg

    • @himanshukaushik9223
      @himanshukaushik9223 Рік тому

      @@bhargavanand7325 thanks bro

  • @tarun1684
    @tarun1684 Місяць тому +9

    I gonna fainted..after watching at last that this was easy section..what will happen god in future..bcz I m beginner and finding difficulties 😢😢

  • @puneetnj
    @puneetnj Рік тому +22

    Right Shift Solution(Leetcode 189):
    k = k % arr.length;
    reverse(arr, 0, arr.length-1-k);
    reverse(arr, arr.length-1-k+1, arr.length-1);

    reverse(arr, 0, arr.length-1);

    • @hari7939
      @hari7939 Рік тому +1

      Just do k=k%n, k=n-k!! as simple as that

  • @itz_fuzzy6941
    @itz_fuzzy6941 7 місяців тому +24

    Munnawar Faaroqi explaining dsa questions...😂😂😂😂😂

  • @subhajitmandal1869
    @subhajitmandal1869 2 місяці тому +4

    "THERE IS A ONLY ONE KING IN DSA"

  • @arnab_16
    @arnab_16 9 місяців тому +24

    not only your dsa knowledge is good also your communication skill, voice tone , body movement is superb 🥰🥰

  • @SatwikSharma21bcs080
    @SatwikSharma21bcs080 3 місяці тому +3


    Learn left rotation of an array by one place
    00:03

    Understand the difference between space used and extra space in algorithms.
    06:07

    Learn how to left rotate an array in C++ or Java
    18:42

    Left rotate an array by D places
    24:15

    Use two pointer approach to move zeros to the end of an array.
    34:25

    Summary of array problems
    40:19

    Implementing a pointer approach to find the union of two sorted arrays.
    50:18

    Union and intersection of sorted arrays
    55:30

    Optimize time complexity using two pointer approach
    1:06:02

    Optimal solution for intersection of two sorted arrays
    Click to expand
    1:11:14

  • @chiragbirla5606
    @chiragbirla5606 Рік тому +9

    One thing striver.....
    In the sheet for the same question as we know there is a link attached for the gfg version and for the leetcode version but in some cases leetcode version is harder and sometimes gfg version.
    So please pick the hard problem for that

  • @codeby_chance6728
    @codeby_chance6728 3 місяці тому +10

    Striver please bring string series. Much needed

  • @Lucifer-xt7un
    @Lucifer-xt7un Рік тому +9

    Bro requesting you please upload videos alternatively as promised bcoz completely relaying on you for placements🥺🥺

    • @NithinvKumar-uk2he
      @NithinvKumar-uk2he Рік тому

      Yes you are right, striver bro please we are requesting you please upload the video as promised by you 🙏

    • @takeUforward
      @takeUforward  Рік тому +14

      I am keeping my promise, if you see am covering 5 problems in one video. So recording such a long video, then editing it, then uploading it takes time. I can upload daily, if you want me to to cover one video.

    • @NithinvKumar-uk2he
      @NithinvKumar-uk2he Рік тому +2

      @@takeUforward ok brother thanks for helping us .we are waiting for upcoming videos ☺️

    • @harshavardhan184
      @harshavardhan184 Рік тому

      True

    • @Lucifer-xt7un
      @Lucifer-xt7un Рік тому

      @@takeUforward understood ur pain bro thanks for everything.

  • @himadriroy7980
    @himadriroy7980 11 місяців тому +13

    I did not only enjoy the lecture but loved it too. Thanks Striver Bhaiya for explaining so lucidly. ❤

  • @MotivexBeastt
    @MotivexBeastt 8 місяців тому +10

    Striver is the lion of this community

  • @saswatrath4646
    @saswatrath4646 6 місяців тому +2

    //HOMEWORK FOR ROTATE RIGHT
    class Solution {
    public:
    void rotate(vector& nums, int k) {
    k = k % (nums.size());
    int n = nums.size();
    vectortemp;
    for(int i = n-k; i

  • @hey-cg3fv
    @hey-cg3fv Рік тому +3

    Move zeroes question another approach with O(1) space
    int j=0;
    // Firstly taking all non- zeroes number
    for(int i=0;i O(1)

  • @Narasimha-ms2up
    @Narasimha-ms2up Рік тому +2

    Thank you brother if your infront of me i would have clean your feet with water 💦 because I ever seen tutor like you and your tutorials are more than a 10 lak rupees ❤❤❤

  • @senseiAree
    @senseiAree Рік тому +8

    I always thought that the optimal solution to this code was the hashset one that you did in the middle part. It blew my mind when I saw the optimal one... And that too so easy man... Wow .. just wow to your observation ❤

  • @techwech8265
    @techwech8265 9 місяців тому +2

    Wrong Logic at 40:46 (Move Zeros to End ) what if the Array element is {1,0} at the first iteration your j=1 means you found zero element at index 1, Now you found first non zero element at 0 index you swap nums[i] and nums[j] now array look like this {0,1} and j is now 2 since i=1 means loop will run one more time bcz i is less than size of array i-e 2, Again you find non-zero element at index 1 i-e 1 you try to swap nums[1] and nums[2] You will end up with an Runtime error ArrayIndexOutOfBound.

  • @architha4305
    @architha4305 3 місяці тому +7

    for the first time i guessed which concept can be used it might sound like a small thing but as a beginner who is just learning to code actually implementing codes without any reference and knowing which loop is used is really HUGE for me. THANK YOU SOO MUCH

    • @zenmonk29
      @zenmonk29 2 місяці тому

      same for me. feels like a big leap

    • @KrishnaKumar-b4m9p
      @KrishnaKumar-b4m9p 29 днів тому

      hi in which question u implemented the code yourself? i am having a hard time in writing code for right rotate an arrray ky k places on my own...

  • @vanshika_gupta1524
    @vanshika_gupta1524 6 місяців тому +2

    bhaiya intersection of two array waale concept m jab aapne final arry nikala tab aapne 3 ko two time liya h but intersection mein toh common element aata h aur vo bhi one time hi aata h?

  • @Akash-yr2if
    @Akash-yr2if Рік тому +4

    Hey Striver,
    Won't this work fine for the Move Zero's to the end :
    void moveZeroes(vector& nums) {
    int i=0;
    for(int j=0;j

    • @arunimmalviya1605
      @arunimmalviya1605 Рік тому

      How do you know if the first index i.e. i=0, will always going to be zero?
      Also you cannot initiate j from 0, if arr[] = {1,2,3,0}, both I and j will point to 1 and since they both are non zero elements, you are swapping them with themselves only.

  • @amanrohilla6659
    @amanrohilla6659 4 місяці тому +1

    in ques rotating arr by k places last for loop n-d = 6-3 =3 but ath 43 index there is alreeady 7 .

  • @AbhishekSingh-rp6hp
    @AbhishekSingh-rp6hp Рік тому +7

    Your course is awesome wish I could have found your course a year before currently I am in 3rd year of my clg 😊

  • @adarsh_endeavour
    @adarsh_endeavour 11 місяців тому +1

    Bhiya arrays k dsa m list kaa zikrr kyu krna, list have method to add but arrays don't have.. I know you are well versed in your profession but ek dikkat h beginners ko hoti h when they heard About list and trying to implement it in arrays😢, just my point of view hope you understand

  • @aswinsrikanth408
    @aswinsrikanth408 Рік тому +8

    Understood and what an explanation
    Love your explanation bro, just continue doing what you are doing for the coding community
    Amazing person

  • @veerendra3911
    @veerendra3911 2 місяці тому +1

    GUYS THIS MY BRUTE FORCE APPROACH PLEASE TELL ME IS RIGHT OR WRONG 🙏🙏🙏
    import java.util.ArrayList;
    public class Step3_Arrays {
    static void EndZeros(int []arr,int n){
    ArrayList TempArraylist1=new ArrayList();
    ArrayList TempArraylist2=new ArrayList();
    for(int i=0;i

  • @anilkumar4200
    @anilkumar4200 Рік тому +3

    Why bro Why, Brute force->better-> optimal, Its really a big effort. thanks for such wonderful video series. Love the way u explain.

  • @mukeshmanral1327
    @mukeshmanral1327 8 місяців тому +2

    wrong code for intersection of two arrays will not work in leetcode

  • @satishmaurya7032
    @satishmaurya7032 Рік тому +9

    What a wonderful video, it was vey good. Your way of teaching concepts through questions is best.

  • @AbjSir
    @AbjSir Рік тому +1

    i think i got a better solution for move zeroes to end, If im wrong please correct me
    void Solve2(int arr[] , int arrl) //O(n) with space complexity O(1) {Only 1 variable }
    {
    int counter =0;
    for(int i=0;i

  • @Str13011
    @Str13011 Рік тому +10

    Thank you for all the tutorials,
    Please keep supporting from starting to get good placement by the help of your tutorials!

  • @chandusweety3341
    @chandusweety3341 3 місяці тому +2

    class Solution {
    public void rotate(int[] nums, int k) {
    int n=nums.length;
    k=k%n;
    reverse(nums,n-k,n-1);
    reverse(nums,0,n-k-1);
    reverse(nums,0,n-1);
    }
    void reverse(int arr[],int start,int end)
    {
    while(start

  • @ParasSharma-mf8or
    @ParasSharma-mf8or Рік тому +9

    0:00 Introduction of course
    01:01 Left rotate an Array by 1 Place
    04:38 Pseudo code
    07:17 Code-compiler
    08:05 Left rotate an Array by D Places
    14:38 Pseudo code
    20:18 Code-compiler
    27:13 Move zeroes to end
    37:41 Pseudo code
    40:30 Code-compiler
    41:05 Linear search
    42:01 Pseudo code
    42:43 Code-compiler
    43:04 Union of two sorted array
    53:33 Code-compiler
    59:05 Intersection of two sorted array
    1:09:30 Code-compiler

  • @Mgamer1524
    @Mgamer1524 21 день тому +1

    void reverse(int arr[],int start,int end){
    int temp;
    while(start

  • @uttamvashisth4072
    @uttamvashisth4072 Рік тому +62

    Salute to this consistency.

  • @yashsaxena7787
    @yashsaxena7787 8 місяців тому +2

    Great! btw in the worst case of Intersection, the space complexity would be O(n1>n2?n2:n1) because whichever one is smaller matches completely with the other array and ends. P.S.- the space is for returning the answer and not solution.

  • @kushagramishra5638
    @kushagramishra5638 Рік тому +36

    Understood 😄.
    Logic used for right rotate the array k places is that instead of rotating array right by k places I have rotated it n-k places to left .
    so code for this is -
    class Solution {
    public:
    void rotate(vector& nums, int k) {
    int n = nums.size();
    k = k % n;
    reverse(nums.begin(),nums.begin()+(n-k));
    reverse(nums.begin()+(n-k),nums.end());
    reverse(nums.begin(),nums.end());
    }
    };

    • @Miracle_shaw
      @Miracle_shaw Рік тому +2

      hey, me also applied same approach

    • @itzmartin20
      @itzmartin20 Рік тому +2

      same here

    • @jatinukey4062
      @jatinukey4062 10 місяців тому +2

      Exactly right

    • @expl0itr
      @expl0itr 5 місяців тому +3

      reverse(nums.begin(), nums.end());
      reverse(nums.begin(), nums.begin() + k);
      reverse(nums.begin() + k, nums.end());
      u can do this also.

  • @Ansarieng
    @Ansarieng Рік тому +1

    right rotate
    any index i with d right rotation then new index will be
    (i+d)%n where n is the length of an array

  • @sangeetasharma8574
    @sangeetasharma8574 Рік тому +12

    Thank You Striver for such an effort.
    Your explanations and teaching is really amazing.
    Its really Valuable /priceless content :)

  • @anshumanraturi
    @anshumanraturi 2 місяці тому +1

    why my output is not coming?????
    349. Intersection of Two Arrays
    class Solution {
    public:
    vector intersection(vector& nums1, vector& nums2) {
    int i=0;
    int j=0;
    int n1=nums1.size();
    int n2=nums2.size();
    vector ans;
    while(i

  • @chidambarjoshi3470
    @chidambarjoshi3470 Рік тому +4

    In the brute force approach of union of arrays, you could have directly returned the set instead of taking a temp vector and copying it all over, because the set would only take the single copies of the elements are the time of insertion

    • @nashimbiswakarma
      @nashimbiswakarma Рік тому +6

      bro you are right but most of the time they i.e., dsa practicing websites expect you to return the result in the form of array(for c++ vector and for Java ArrayList).

  • @SauravSingh-xm6nn
    @SauravSingh-xm6nn 4 місяці тому +1

    how about using recursion for left rotation at 0:24:00 ???
    void left_rotate_better(int arr[],int n,int d)
    {
    d=d%n;
    if(d==0)
    return;
    int temp=arr[0];
    for(int i=1;i

  • @harshavardhan184
    @harshavardhan184 Рік тому +3

    This much consistency is all we needed bhaiya🤞🤞🤞kudos to you

  • @ajaymishra1511
    @ajaymishra1511 4 місяці тому +1

    it took me an hour to do rotate by K problem now even i don't remember how i did this
    ```js
    function a(len,jk){
    if(jk >= 0) return jk
    if(jk < 0){
    const b = len - Math.abs(jk)
    return a(len, b)
    }
    }
    var rotate = function(nums, k) {
    const copyArr = [...nums]
    for(let j = 0; j < nums.length; j++){
    const replacement = a(nums.length, j-k)
    nums[j] = copyArr[replacement]
    }
    return nums
    };
    ~```

  • @AdityaKumar-be7hx
    @AdityaKumar-be7hx Рік тому +9

    Actually, there is another optimal solution for moving zeroes problem. It is similar to the function we use in quicksort algorithm (quickselect part for arranging elements around pivot).
    void Optimal2(̀vector& nums) {
    int j=-1, n=nums.size();
    for(int i=0; i

    • @nagame859
      @nagame859 Рік тому +2

      Exceptionally concise👏

    • @KrishnaKumar-b4m9p
      @KrishnaKumar-b4m9p 28 днів тому

      can u explain how the swap function works here. we want to move all the zeroes in the end how nums[++j]; ????

    • @KrishnaKumar-b4m9p
      @KrishnaKumar-b4m9p 28 днів тому

      u wanna see my solution??
      void Zero_at_last(int arr[], int n){
      int i=0;

      for (int j = 1; j < n; j++)
      {
      if(arr[i]==0 ){
      if(arr[j]==0){
      continue;
      }
      swap(arr[i], arr[j]);
      }
      i++;
      // cout

  • @ritochitghosh1753
    @ritochitghosh1753 7 місяців тому +1

    Assignment:-
    "Right rotate an array by k places"
    class Solution {
    public void rotate(int[] nums, int k) {
    int[] ans = new int[nums.length];
    for(int i = 0; i < nums.length; i++){
    ans[(i+k) % nums.length] = nums[i];
    }
    for(int i = 0; i < nums.length; i++){
    nums[i] = ans[i];
    }
    }
    }

  • @suriyavs8685
    @suriyavs8685 Рік тому +3

    Hey striver wonderful video. I have a query. The code will be applicable only if the vector or array is sorted in the intersection part. So do we need to sort it again or is there any other optimal approach ?

  • @Anil_Rebel
    @Anil_Rebel Місяць тому +1

    class Solution {
    public void rotate(int[] nums, int d) {
    int n=nums.length;
    d=d%n;
    int l=0,r=n-d-1;
    for(int i=0;i

  • @sikanddhingra5835
    @sikanddhingra5835 8 місяців тому +3

    bhai ki pehli video lgayi h ye mene. bohut maja aya, dil garden garden hogya bilkul. thanku bhai.😁

  • @anishchhabra6085
    @anishchhabra6085 11 місяців тому +1

    Simple code for right rotating an array (Python):
    new_arr = [1,2,3,4,5,6,7]
    d = 22
    n = len(new_arr)
    d = d%size
    left_arr = new_arr[n-d:]
    right_arr = new_arr[:n-d]
    new_arr = left_arr + right_arr

  • @kirankawade3424
    @kirankawade3424 Рік тому +3

    Thank you so much for sharing the content free of cost!! very nicely explained too😘

  • @harigs72
    @harigs72 Місяць тому +1

    26:02 here the time complexity is 0(N);not O(2N)
    In reverse
    The time complexity is n/2;
    Not 0(N);
    Correct me if I am wrong

  • @vivek5144
    @vivek5144 Рік тому +8

    Hi Striver can you please please upload the notes which you are explaining it will be very helpful 🙏🙏

    • @RamPageMMA
      @RamPageMMA 3 місяці тому

      Take your own notes

  • @abhishekjadhav2250
    @abhishekjadhav2250 Місяць тому +1

    conscise solution for move zeroes to end
    class Solution {
    public:
    void moveZeroes(vector& nums) {
    int n = nums.size();
    int j = 0;
    for (int i = 0; i < n; i++) {
    if (nums[i] != 0) {
    swap(nums[i], nums[j]);
    j++;
    }
    }
    }
    };

  • @rish7394
    @rish7394 Рік тому +3

    Bhaiya kab tk poora hoga 🥲

    • @bishalkundu7592
      @bishalkundu7592 Рік тому

      In a previous post bhaiya said that he will try to cover this course in the next 3 months.

  • @lwnflash2123
    @lwnflash2123 5 місяців тому +1

    For the instersection question,why don't you consider temp array/vector space in addition to visted array for space complexity(brute force) ,because that too is used?.And for the optimal solution space complexity must be O(min(n1,n2)) if all elements are common??...

  • @sudhirdaga3453
    @sudhirdaga3453 Рік тому +4

    Right rotate array by d places is equivalent to left rotate array by n-d places.
    void rightRotateArrayOptimal(int arr[],int n,int d){
    d = d%n;
    d = n-d;
    reverse(arr,arr+d);
    reverse(arr+d,arr+n);
    reverse(arr,arr+n);
    }

    • @ayushmittal9666
      @ayushmittal9666 Рік тому

      It's wrong it should be
      d=n%d
      reverse(a,a+n)
      reverse (a,a+n-d)
      reverse (a+n-d,a+n)

  • @chirlasowjanyareddy
    @chirlasowjanyareddy Рік тому +1

    move all zeroes to end of array this is same approch but some lines of code are different from that of strivers!!!!!
    void pushZerosToEnd(int arr[], int n)
    {
    // code here
    int i=0;
    int j=i+1;
    while(j

  • @rohanyadav6295
    @rohanyadav6295 8 місяців тому +1

    😢sir, you are solving each problem with vectors or stack,,
    As we are doing array problems,,,
    So, solve it using array na

  • @dom47
    @dom47 Рік тому +1

    got confused with move zero to end problem
    easy solution
    class Solution{
    public:
    void pushZerosToEnd(int arr[], int n) {
    // code here
    int i=0,j=0;
    //int size=n;
    while( i

  • @rohandhalpe9084
    @rohandhalpe9084 Рік тому +2

    Bhaiyaa ji please upload videos as fast as possible .Our placement season starting soon in June.💫💫💫💫💫💫💫💫💫💫💫💫💫💫

  • @KrishnaKumar-b4m9p
    @KrishnaKumar-b4m9p 26 днів тому +1

    last question optimal solution was so cute. Even cuter than my imaginary girlfriend. Just the difference is I can't understand her.

  • @himanshurawat2812
    @himanshurawat2812 2 місяці тому +1

    Space complexity of Intersection problem would be O(min(n,m)) for optimal solution

  • @ananthegde2107
    @ananthegde2107 Рік тому +1

    Brute Force Solution :
    TC : O(n) + O( n-arr.size() ) = O( 2n - arr.size() );
    SC : O(n);
    #include
    vector moveZeros(int n, vector a) {
    // Write your code here.
    vectorarr(0,n);

    for(int i=0;i

  • @Bzgwk
    @Bzgwk 11 місяців тому +1

    RIGHT ROTATE BY K PLACES
    class Solution {
    public void rot(int[] arr, int s, int e){
    while(s

    • @aanchalupadhyay3971
      @aanchalupadhyay3971 11 місяців тому

      why am I getting the 'this is taking too long' for the intersection problem using the first approach? can you please explain?
      #include
      vector findArrayIntersection(vector &arr1, int n, vector &arr2, int m)
      {

      vector visited(m,0);
      vector arr;
      for(int i=0;i

  • @shreyanshgamer1079
    @shreyanshgamer1079 3 місяці тому +1

    How to know our solution is otiomal solution or better solution
    We have to memorize optimal solution or what!!?

  • @debanshupati207
    @debanshupati207 Місяць тому +1

    at 16:40 the i=k striver had taken should be n-d right as k is not defined or we can use n-d in k

  • @Josuke217
    @Josuke217 2 місяці тому +1

    For the last question, I got the optimal solution before brute force approach :/

  • @dhruvpatel0503
    @dhruvpatel0503 6 місяців тому +1

    In move zeroes to end problem we can use this also...,
    int y=0;
    for(int i=0;i

  • @saketjaiswal9381
    @saketjaiswal9381 Рік тому +1

    sir u write code and the code runs in one go are u hacker or wottt

  • @harshitrautela6585
    @harshitrautela6585 8 місяців тому +1

    🎯 Key Takeaways for quick navigation:
    00:56 🔄 *Left rotate an array by one place involves moving elements one place ahead. Optimal solution involves in-place rotation without using a new array.*
    05:26 ⏰ *Time complexity of left rotation by D places is O(N+D). Extra space complexity is O(D) due to the use of a temporary array.*
    12:20 🤔 *When left rotating an array by D places, D can be reduced to D mod N, as multiples of N rotations bring the array back to its original state.*
    20:37 ⚙️ *Optimal solution avoids using extra space by reversing the array elements in three steps: reverse the whole array, reverse the first D elements, and reverse the remaining N-D elements.*
    23:04 🔄 *The speaker explains a method to rotate an array to the left by a given number of places using three reverse operations.*
    27:12 🔄 *The problem of moving zeros to the end of an array is introduced, and a Brute Force approach is discussed, involving identifying non-zero numbers, moving them to the front, and filling the remaining positions with zeros.*
    33:45 🔄 *An optimal solution for moving zeros to the end is presented, using a two-pointer approach to efficiently move non-zero elements to the front while iterating through the array.*
    40:57 🧐 *Linear search is introduced as the problem of finding the first occurrence of a given number in a sorted array. The speaker presents a Brute Force approach involving iterating through the array and checking for equality.*
    43:04 🧑‍🤝‍🧑 *The problem of finding the union of two sorted arrays is discussed, emphasizing the need for an optimal solution. The speaker starts with a Brute Force approach involving combining both arrays and removing duplicates.*
    44:40 🧭 *Brute force approach for finding the union of two sorted arrays involves using a set data structure to ensure uniqueness and sorted order.*
    50:22 🚥 *Optimal approach for finding the union of two sorted arrays uses a two-pointer approach to efficiently iterate through both arrays, considering unique elements.*
    53:39 🕰️ *The time complexity of the optimal approach for finding the union is O(N1 + N2), where N1 and N2 are the sizes of the two arrays.*
    59:00 🤝 *Brute force approach for finding the intersection of two sorted arrays involves checking for corresponding elements and using a visited array to ensure uniqueness.*
    01:05:59 🔄 *The time complexity of the brute force approach for finding the intersection is O(N1 * N2), where N1 and N2 are the sizes of the two arrays.*
    01:06:13 🧠 *Brute Force approach for array intersection involves checking every element in both arrays for a match, leading to a time complexity of O(N1 * N2).*
    01:07:32 🔄 *Optimal solution for intersection utilizes a two-pointer approach due to sorted arrays, resulting in a time complexity of O(N1 + N2) and no extra space usage.*
    01:09:48 🤖 *Implementing the optimal solution involves iterating through both arrays, comparing elements, and building the intersection array with minimal space complexity.*
    Made with HARPA AI

  • @gyana67644
    @gyana67644 Рік тому +2

    Bro please add Java code in side while you are writing c++ code.

  • @PriyaranjanSingh-g2b
    @PriyaranjanSingh-g2b 11 місяців тому +1

    In MAANG interview generally the coding round is of 45 min and they ask 2-3 LC medium questions.So, approx. 15 min(discuss + solve) for 1 question if 2 medium LC question they asked. So, if we know optimal solution then discussing on brute force approach will cost us. So, any suggestion here ?

  • @sahilmishra9285
    @sahilmishra9285 Рік тому +1

    //RIGHT ROTATE
    class Solution {
    public:
    void reverse(vector &nums,int start,int end){
    while(start

  • @shreyasr2885
    @shreyasr2885 12 днів тому +1

    he never skips anything, Patience is next level!!

  • @Aj-rh4jv
    @Aj-rh4jv Рік тому +1

    right rotate array k places
    brute and optimal approach
    class Solution {
    public:
    void rotate(vector& nums, int k) {
    // brute force approach
    // int n = nums.size();
    // k = k % n;
    // int temp[n];
    // for(int i = n - k; i < n; i++) {
    // temp[i - (n - k)] = nums[i];
    // }
    // for(int i = n - k - 1; i >= 0; i--) {
    // nums[i + k] = nums[i];
    // }
    // for(int i = 0; i < k; i++) {
    // nums[i] = temp[i];
    // }
    // optimal approach
    k=k%nums.size();
    reverse(nums.begin(),nums.begin()+(nums.size()-k));
    reverse(nums.begin()+(nums.size()-k),nums.end());

    reverse(nums.begin(),nums.end());
    }
    };

  • @tanishagarwal5992
    @tanishagarwal5992 9 місяців тому +1

    In the three reversals, we are doing iterations just for half the time i.e O(d/2) + O((n-d)/2) + O (n/2) making it O(n) so TC as well as SC of optimal solution is better than the better solution.

  • @yokeswarans2903
    @yokeswarans2903 Рік тому +1

    class Solution {
    public:
    void rotate(vector& nums, int k) {
    k=k % nums.size();
    vector temp(nums.size());
    for(int i=0;i

  • @suprithsk9595
    @suprithsk9595 Рік тому +1

    left rotate without extra space:
    i=0
    for j in range(1,n):
    arr[i],arr[j]=arr[j],arr[i]
    i+=1
    return arr

  • @tushartyagi4965
    @tushartyagi4965 11 місяців тому +1

    Rotate right ---->>>>
    void rotate(vector& nums, int k) {
    vector temp(nums.size());
    for(int i=0; i< nums.size(); i++)
    {
    temp[(i+k)%nums.size()] = nums[i];
    }
    nums=temp;

    }

  • @priyanshusinha5673
    @priyanshusinha5673 4 місяці тому +1

    in the brute force solution of the interscetion of two array , the code will be correct even if i dont use break statement , only the number of iterations will increase , but in the given problem link , its not showing the correct answer in that case (if i am not using the break case)

  • @sanjaykrishna689
    @sanjaykrishna689 6 місяців тому +1

    left rotation to right rotation: simple approach is to think oppositely to left rotation.
    Shift all elements right of d and add to temp and push it.
    Just reverse the order of the reverse statements used in left rotation!.

  • @factgamers2392
    @factgamers2392 3 місяці тому +1

    Understood but, 1:10:36 was 💀, no offence striver bhaiya

  • @ruhiawasthi9
    @ruhiawasthi9 2 місяці тому +1

    I have written this approach for rotating the array to the right by k steps : void rotate(vector& nums, int k) {
    k%=nums.size();

    reverse(nums.begin(), nums.end());

    reverse(nums.begin(), nums.begin() + k);

    reverse(nums.begin()+k, nums.end());


    }

  • @mohit84604
    @mohit84604 Рік тому +1

    OPTIMAL CODE FOR MOVE ZOEROES TO END IN JAVASCRIPT :- let j = -1;
    for (let i = 0; i < array.length; i++) {
    if (array[i] == 0 && j == -1) {
    j = i;
    }
    if (array[i] != 0 && j != -1) {
    swap(array, i, j);
    j++;
    }
    }

  • @crazyxyzk143
    @crazyxyzk143 Рік тому +1

    // right rotation by k places
    int k;// no of rotation
    cin>>k;
    int d = k%n;
    reverse(arr,arr+(n-d));
    reverse(arr+(n-d),arr+n);
    reverse(arr,arr+n);
    for(int i: arr){
    cout

  • @UECSoumyaRay
    @UECSoumyaRay Рік тому +2

    code for right-rotation:
    void rotate(vector& nums, int k) {
    int n = nums.size();
    if (k > n) {
    k = k % n;
    }
    reverse(nums.begin(), nums.begin()+n-k);
    reverse(nums.begin()+(n-k),nums.begin()+n );
    reverse(nums.begin(), nums.begin()+n);
    }

    • @crazybro4383
      @crazybro4383 7 місяців тому

      Hii brother can you pls explain me why time complexity of intersection is O(1) and not O(n), where n is the size of larger sized array. Time stamp- 1:12:01

  • @chinmaymathakari9704
    @chinmaymathakari9704 10 місяців тому +2

    Thank you so much for taking out time of your busy schedule. Can you please make such videos on Trees as well ? That will be a great help. Thanks a lot😊😊

  • @senpaidawwwggg
    @senpaidawwwggg Рік тому +2

    two pointer approach is great ...

  • @DSAMADESIMPLE
    @DSAMADESIMPLE Рік тому +1

    for right rotate:- just one line using inbuilt STL
    rotate(arr,arr+(n-k%n),arr+n);
    for Left rotate:- just one line using inbuilt STL
    rotate(arr,arr+k%n,arr+n);

  • @ArnabBhadra02
    @ArnabBhadra02 Рік тому +1

    void right_rotate(int arr[],int n,int d){
    reverse(arr,arr+n-d);
    reverse(arr+n-d,arr+n);
    reverse(arr,arr+n);
    }

  • @curs3m4rk
    @curs3m4rk 6 місяців тому +1

    I recommend everyone to solve the linked leetcode problems attached as well, as the code needs to be tweaked little bit . So u can understand and do by yourself
    Great approaches Striver. thanks

  • @stutimishra2005
    @stutimishra2005 6 місяців тому +1

    understood😊

  • @isheep9025
    @isheep9025 Рік тому +1

    union of 2 arrays can also be done without using extra space