Left Rotation | HackerRank | Python | Data structure | Array | Interview

Поділитися
Вставка
  • Опубліковано 11 жов 2024
  • This video is about HackerRank Left Rotation problem.
    A left rotation operation on an array of size n shifts each of the array's elements 1 unit to the left. For example, if 2 left rotations are performed on array [1,2,3,4,5], then the array would become [3,4,5,1,2].
    Given an array of n integers and a number, d, perform d left rotations on the array. Then print the updated array as a single line of space-separated integers.
    Problem:www.hackerrank...
    sample code: letstacle.com/...
    Follow us on:
    Whatsapp:
    chat.whatsapp....
    Telegram:
    t.me/joinchat/...
    For Python homework help visit our sponsor site
    letstacle.com/...
    You can support via UPI : sattujaiswal@okhdfcbank
    For 1 to 1 Python Tutoring
    letstacle.com/...
    Email: help@letstacle.com
    Facebook:
    / 779620502848988
    / codingcart
    Linkedin:
    / satyendra-jaiswal-9035...
    / codingcart
    Instagram:
    / codingcart
    #HackerRank #Array #DataStructure #Interview

КОМЕНТАРІ • 55

  • @codingcart
    @codingcart  4 роки тому +2

    Hey Programmer/Learner, if you have any better solution feel free to post in comment section.

    • @priyankachoudhary934
      @priyankachoudhary934 4 роки тому +1

      new_arr = a[(d%n) : ] + a[0:(d%n) ]
      it works the same coz by adding n to d it has no effect on the remainder , we can directly use d%n.
      nice video.

    • @codingcart
      @codingcart  4 роки тому

      @@priyankachoudhary934 Thanks @Priyanka. Yes we can directly use d%n coz when you simplify (n+d)%n you will get d%n.
      Keep Learning Keep Supporting :)

    • @priyankachoudhary934
      @priyankachoudhary934 4 роки тому

      @@codingcart yess :)

    • @whatyoudidntknow1088
      @whatyoudidntknow1088 4 роки тому +3

      arr = arr[d:] + arr[0:d] also works ! Thanks a lot sir❤️❤️🙏

  • @abhilashrudra4848
    @abhilashrudra4848 4 роки тому +4

    I have never seen such nice explanation of problems!

  • @zedor1553
    @zedor1553 3 роки тому +3

    beautiful and simple thinking and teaching :) thanks!

  • @sachinsingh-jg9lx
    @sachinsingh-jg9lx 4 роки тому +5

    Best explanation for rotational array

  • @shubhamkumarrohit6574
    @shubhamkumarrohit6574 3 роки тому +5

    great comeback, better is a[d:] + a[0:d]

  • @tanmaytomar3322
    @tanmaytomar3322 4 роки тому +4

    Wow man. The last trick was simply awesome

  • @optimistichomosapien7484
    @optimistichomosapien7484 2 роки тому +2

    Great explanation

  • @shadow_self8564
    @shadow_self8564 4 роки тому +2

    Well Explained briefly. Great video

  • @itswiftyy
    @itswiftyy 3 роки тому +5

    A very easy and simple solution that works is this:
    for i in range(d):
    a.append(a.pop(0))
    return a

    • @edunery
      @edunery 2 роки тому

      this is O(n² ) due to pop() in position 0 being a O(n) operation

  • @loveysingh9913
    @loveysingh9913 4 роки тому +4

    waaoo such a great logic thank u sooo much sir

  • @leenabhardwaj4067
    @leenabhardwaj4067 4 роки тому +3

    Good going👍

  • @shaktiprasannapani9941
    @shaktiprasannapani9941 2 роки тому +1

    i have tried to make it simple...it is working..
    list1 = [0] * len(arr)
    for i in range(len(arr)):
    list1[i - d] = arr[i]
    return list1

  • @sahilbisht3661
    @sahilbisht3661 4 роки тому +3

    sir this video is simply awesome but please tell me how you make this logic ? how i came to know that now i have to do (n+d)%n how u came across this logic and how to made it please tell us so we can do question from our own!! thanks and a very good explanation thank you

    • @codingcart
      @codingcart  4 роки тому +3

      Glad u liked it!!
      Sunny, it's all about practising.
      As a beginner, First you have to apply Brute Force Algorithms and then after go for optimization and during this process try to print the value at each step and see how actually the things are going, here you will learn so many things like you may get few patterns which are repeating as you saw in this problem and many more things.
      But the key part is practising and solving the problem by different different logic.
      Try these 2 problem.
      ttps://ua-cam.com/video/Ms85EsPu7nc/v-deo.html
      ua-cam.com/video/kHcv1tGnF5Y/v-deo.html
      Hope this helps :)

    • @sahilbisht3661
      @sahilbisht3661 4 роки тому

      Coding Cart thanks a lot buddy ❤️ hope to see 1M on this channel soon 🔥

  • @hellyshah6278
    @hellyshah6278 3 роки тому

    What is rstrip?? And why u r taking for _ ??

  • @soumyakantibanerjee_0654
    @soumyakantibanerjee_0654 4 роки тому +1

    Also plz check out New Year Chaos(hackerrank-->practice-->interview preparation kit-->new year chaos)

    • @codingcart
      @codingcart  4 роки тому

      sure

    • @codingcart
      @codingcart  4 роки тому

      check this one ua-cam.com/video/VT-ChOqUsBM/v-deo.html

  • @NCCSAyesha
    @NCCSAyesha 2 роки тому +1

    i think index=n-(k%n) and retturn index will pass all the test cases

  • @chitneedihemanthsaikumar7511
    @chitneedihemanthsaikumar7511 4 роки тому +1

    Why can't we use range(d%n)? if d=n=10^5 then loop won't execute

    • @codingcart
      @codingcart  4 роки тому

      Just try once from your end. You will get the solution yourself :)

  • @holl11day66
    @holl11day66 4 роки тому +1

    what does it mean: a=a[1:]+a[0:1]? Can you explain me?

    • @codingcart
      @codingcart  4 роки тому +3

      a[1:] will slice value from index 1 to last and a[0:1] will slice the value from 0th index to 1(excluded) i.e the very 1st value.
      eg: a=[4,5,3,6,7]
      a[1:] will give [5,3,6,7]
      a[0:1] will give [4]
      Hope this helps :)

  • @urmigori9153
    @urmigori9153 4 роки тому

    How can I unpack using function? Since I have to end it with return statement I am unable to unpack it.

  • @soumyakantibanerjee_0654
    @soumyakantibanerjee_0654 4 роки тому +1

    Plz make a vedio on perfection quotients problem (hackerrank-->compete-->project euler-->#241perfection quotients)..I don't know python☹️ can u plzz solve it in Java🙏🙏

  • @sorrowseal
    @sorrowseal 4 роки тому

    I guess one thing is to also keep in mind, if the length of list equals the rotations, it will put back the list as is, so just returned it.

  • @sivapriya3276
    @sivapriya3276 3 роки тому

    what can we do if they give in the form of recursion like
    def rotLeft(a, d):
    if __name__ == '__main__':

  • @benjaminmoon4554
    @benjaminmoon4554 4 роки тому +1

    Looks like you can use a[d%n:] + a[0:d%n] and get the same result because (d+n)%n is the same as d%n.

    • @codingcart
      @codingcart  4 роки тому +1

      Correct ..You can modify that i have written coz i was formulating the pattern 😊

    • @benjaminmoon4554
      @benjaminmoon4554 4 роки тому +1

      @@codingcart Thank you very much for this video and your breakdown of the logic.

  • @gamingtweaks3065
    @gamingtweaks3065 4 роки тому +1

    simply type -- return (arr[d:]+arr[:d])

    • @codingcart
      @codingcart  4 роки тому

      Correct .But here i have formulated the logic step wise so that everyone can easily understand :)

    • @gamingtweaks3065
      @gamingtweaks3065 4 роки тому

      @@codingcart nice sir, but I want to know how did you develop logic for each question, I just Python for 1 month and now I am hacker rank... Can you please tell how do I develop fast logic... How should I think for attempting the problem???

    • @codingcart
      @codingcart  4 роки тому

      It's all about practice and solving alots of different different types of problem.
      By the way you are on good track ..Keep going 😊

    • @gamingtweaks3065
      @gamingtweaks3065 4 роки тому

      @@codingcart ok sir thanks

  • @sush9889
    @sush9889 4 роки тому

    That's my code - It is passing all testcases except 2 testcases. Why Sir ?
    def rotLeft(a, d):
    for i in range(d):
    a=a[1:]+a[0:1]
    return a

    • @codingcart
      @codingcart  4 роки тому

      Hi Sushrut,
      We have explained this case in our 1st solution @11:22. If you see a=a[1:]+a[0:1] for this assignment ,in each iterartion we are performing slicing operation which made this solution to Terminate due to Time out. After this we came with better solution looking the output pattern. Please have a look you will understand easily :).
      Try this problem ua-cam.com/video/XWG58qrcB50/v-deo.html, and hope you dont use slicing here :).

  • @Santana_bold
    @Santana_bold 4 роки тому

    It printed the whole table instead of the last calculation

    • @codingcart
      @codingcart  4 роки тому

      Hi Olga, the requirement was to rotate on the given number, so we have to rotate the list and print the whole list again after n rotation.

    • @Santana_bold
      @Santana_bold 4 роки тому

      Yes, it rotated but it prints every step of rotation instead 1 last step

    • @codingcart
      @codingcart  4 роки тому

      @@Santana_bold No..You can see the code in description section.

  • @gauravtiwari6550
    @gauravtiwari6550 2 роки тому

    Can't we just write?
    a[d:]+a[0:d]
    return a
    My code perfectly solved all the test cases with no tle

    • @codingcart
      @codingcart  2 роки тому +1

      It's the same, I have just generalized the logic as per the explanation 😊

    • @gauravtiwari6550
      @gauravtiwari6550 2 роки тому

      @@codingcart had you make a video on DFS and BFS topics?
      On Monday my hackwithifny exam is scheduled if you can give me with some graph questions link ,that would be a great help.