Reverse a Stack using Recursion

Поділитися
Вставка
  • Опубліковано 3 жов 2024
  • Reverse a Stack in O(1) space using Recursion.
    The Notes that I taught in the video and the Working Code (if any) will be available on my Patreon page under the "Notes, Code And Support" tier.
    Link: / adityaverma
    Video Pdf Notes: / reverse-stack-38641383 .
    ------------------------------------------------------------------------------------------
    Here are some of the gears that I use almost everyday:
    🖊️ : My Pen (Used in videos too): amzn.to/38fKSM1
    👨🏻‍💻 : My Apple Macbook pro: amzn.to/3w8iZh6
    💻 : My gaming laptop: amzn.to/3yjcn23
    📱 : My Ipad: amzn.to/39yEMGS
    ✏️ : My Apple Pencil: amzn.to/3kMnKYf
    🎧 : My Headphones: amzn.to/3kMOzM7
    💺 : My Chair: amzn.to/385weqR
    🛋 : My Table: amzn.to/3kMohtd
    ⏰ : My Clock: amzn.to/3slFUV3
    🙋🏻‍♀️ : My girlfriend: amzn.to/3M6zLDK ¯\_(ツ)_/¯
    PS: While having good gears help you perform efficiently, don’t get under the impression that they will make you successful without any hard work.

КОМЕНТАРІ • 256

  • @sumitkeshav4718
    @sumitkeshav4718 4 роки тому +197

    This IBH is amazing, we dont have to go through the entire recursion, just using base condition and n-1 iteration, and recursion takes care of the rest!!!! Damnnn, seriously recursion does work like magic! Thank you for such brilliant breakdown of recursion, no body teaches anything like these stuff! Kudos

    • @TheAdityaVerma
      @TheAdityaVerma  4 роки тому +55

      Couldn't agree more! Recursion is magic !! 🤯

    • @123akash121
      @123akash121 3 роки тому +14

      @@TheAdityaVerma bro, you're really intelligent, i love how you implemented the abstract idea of "mathemcatical induction" to recursion, just like in mathematical induction in which we just solve for n = 1, and for any n = k, prove n = k+1 is also true so that we dont have to solve for every value between from 1 to k, which is basically what we're doing in your IBH method, for any n = k we just solve for k - 1 and also for the base condition (which is basically the analogy for the n = 1 base condition in mathematical induction concept) so that we dont have to solve for every value between 1 to k (which is what i used to do before in recursion, but now you saved alot of my time through IBH method), you implementation of the mathematical induction idea to real-life application is truly ingenious, you are really smart and a good teacher, subbed.

    • @yuvrajr6491
      @yuvrajr6491 3 роки тому +2

      @@123akash121 bruh everyone into cp knows that tbh, you can read the book "think recursively" which was published in the 80's and same concept is mentioned in it.

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

      @@yuvrajr6491 Thinking Recursively [Roberts 1986-01-17].pdf yahi hai kya?

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

      @@krushnapatil9196 haan bhai

  • @abhishekupadhyay4954
    @abhishekupadhyay4954 4 роки тому +41

    I have gone through these questions and codes about 10 times but until now I have just mugged up the codes. This is the first time I know how recursion works and what's really happening. Thanks a lot for the amazing explanation.Looking forward to backtracking and graph playlist.I think people are eagerly waiting for these two playlists from you.

  • @AnkitKumar-ft5yu
    @AnkitKumar-ft5yu 4 роки тому +14

    This is gold...nobody is teaching like this

  • @sanidhyagupta1906
    @sanidhyagupta1906 4 роки тому +73

    Sir vo Dp vale playlist mei kuch ques reh gaye the like LIS , substring vgrh k , toh unko plz add kr digiye. And thanks a lot bhaiya

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

    The way you build up the logic for complex problems by first hitting at the head of the easier ones is just MIND-BLOWING! YOU LOOK LIKE A MAGICIAN TO ME!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

  • @swapnilraj1375
    @swapnilraj1375 3 роки тому +13

    for the first time I could write my own recursion program :) Thanks a lot for the amazing work. Your teaching style remind me of friend who used to teach all in the early morning before exams.

  • @randomthoughts3533
    @randomthoughts3533 4 роки тому +12

    I did reverse stack using recursion without watching this video. it was somewhat like sort stack and array. previous video helped me to think in this side. Thanks Aditya Appreciate your effort. I wish I could contribute to petreon.

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

      yeah only diff is in sort stack inside insert function we check one extra condition s.top

  • @jaisingh-lb6fp
    @jaisingh-lb6fp 3 роки тому +8

    I was going through each video one by one and implementing it alongside..
    the moment I listened to the problem, I was able to create the solution out of it, and it worked in the first go itself.
    Reason - he teaches you how to think around the problem...not only the solution.
    Thanks, @Aditya Verma..kudos to you!

  • @AnkushKumar-mk8ns
    @AnkushKumar-mk8ns 4 роки тому +9

    Finally, At the middle of this video i myself found the base case for the reverse and insert function even after you forgot to mention that which you gave later in the video of course.
    Thank you bhaiya for this recursion series. 🙏

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

    thank you, i watched your previous videos from playlist and i was able to attempt this one without any difficulty, thanks a lot.

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

    You are making our lives easy
    I have watched many videos of recursion but did not understood the concept till I watched yours. Thanks for your support

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

    Man! The foundation you've built with the previous videos is so good that I didn't even have to watch this video before figuring out the solution!
    //Method to insert the popped element at the beginning
    const insert = (stack, element) =>{
    // Base Condition
    if(stack.length === 0){
    stack.push(element);
    return;
    }
    let top = stack.pop();
    insert(stack, element);
    stack.push(top);
    }
    // Method to reverse the stack
    const reverse = (stack) => {
    //Base Condition
    if(stack.length === 1){
    return;
    }
    let top = stack.pop();
    reverse(stack);
    insert(stack, top);
    }

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

    Was able to solve it by my own , thank you sir , you are providing Gold.

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

    😭😭😭😭bhai aapki wajh se mai rcursion ke problem khud se solve kar paa rha hu aap god ho sach mai

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

    brother hats off to you, your content, your methods can't deny recursion works like magic !!!

  • @shethnisarg3049
    @shethnisarg3049 4 роки тому +45

    Recursion is like god, if we believe in it and do the right things, then we will get the desired output : )

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

    Solved this question without seeing your video, that's the power of your teaching 🔥

    • @hooomansss984
      @hooomansss984 10 місяців тому

      ok bhai

    • @meetjoshi4521
      @meetjoshi4521 10 місяців тому

      so basically in induction we define the structure of code . RIght?

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

    Did this on my own for the very first time . thanks brother .😁

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

    Your explanation very simple 😊😊

  • @ayushisoni3274
    @ayushisoni3274 3 роки тому +2

    Thanks a lot sir!!
    You really made the recursion too simple... Before watching your videos I was not able to code recursive solutions..and now I can easily do it.. also I enjoy solving problems using Recursion.
    Thanks a lot again!

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

    solved the question without watching the video. You made the concept so clear in previous videos .Thanks a lot

  • @shritvaagarwal614
    @shritvaagarwal614 3 роки тому +1

    First recursion code that i wrote myself. Thanks alotttt

  • @yatendraupadhyay2180
    @yatendraupadhyay2180 5 місяців тому

    Bhaiya u explained recursion so well.
    I got the core idea of solving problems using recursion just by watching this lecture.
    Man you are a terrific teacher.
    Thank you for this amazing content.

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

    aisa Khatarnaak confidence aaya hai na RECURRSION me ki pucho mat:) Thanks

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

    Recursion will never be the same for me. Thanks Aditya Verma!

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

    You have realized us that recursion is almost a magic
    Great teaching style

  • @negi7735
    @negi7735 3 роки тому +2

    Thank u a lot sir for sharing your knowledge and tricks. Best ever explanation video on recursion👍

  • @animeshrajput8638
    @animeshrajput8638 4 роки тому +8

    If Recursion is Magic then you are the Magician !!!

  • @11aniketkumar
    @11aniketkumar Місяць тому

    Man, you are amazing, thanks a lot!

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

    great explanation. Thanks

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

    Big thank for this recursion series ....finally i can think how to write recursive code....

  • @tejaswigutta9017
    @tejaswigutta9017 4 роки тому +32

    @Aditya Verma
    Please add time complexity at the end of each problem bro. It'll be useful for those struggling with time complexity

  • @cidmashup7789
    @cidmashup7789 2 роки тому +6

    There's an easy way to achieve this in python:
    def rev(arr):
    if len(arr)==0:
    return
    last=arr[-1]
    arr.pop()
    rev(arr)
    arr.insert(0,last)
    arr=[1,2,3,4]
    rev(arr)
    print(arr)

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

      thanks

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

      Yeah, but you can't simply insert like that in a stack. You need to pop all the elements to insert at the 0th index (Even I did the same before watching the video but realized after watching the induction step :D )
      This has to be the right code
      arr = [10,5,-2,89,7,54]
      class recursion:
      def insert(self,arr,val):
      if len(arr)==0:
      arr.append(val)
      return
      temp = arr.pop()
      self.insert(arr,val)
      arr.append(temp)

      def reverse(self,arr):
      if len(arr)==1:
      return
      val = arr.pop()
      self.reverse(arr)
      self.insert(arr,val)
      return arr
      obj = recursion()
      print(obj.reverse(arr))

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

      pls know what stack is and then code

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

      @@suryavamsikodeti254 i think you can insert in same recursive function reverse with push only.....

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

    Sir You are just great this method works like butter smooth i am just reading topic of video and able to solve for last 3 video and then i watch video at 2x to see if i could have just changed some part of code to make it better. Thanks Sir Again...

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

    never understood recursion like this, hatsoff to u sir

  • @nasamind
    @nasamind 10 місяців тому

    You are great teacher, awesome recursion playlist

  • @hritikbangar4341
    @hritikbangar4341 4 роки тому +24

    I am going to miss the pen tricks that you used to do

    • @TheAdityaVerma
      @TheAdityaVerma  4 роки тому +15

      I still do those trick, they are just not visible now 😓

    • @itsvinayak
      @itsvinayak 4 роки тому +16

      @@TheAdityaVerma please use pen and copy

    • @akhilkumarsingh5041
      @akhilkumarsingh5041 4 роки тому +9

      @@itsvinayak jab bahi ye pen paper se smajhta tha tab bahut acche se samajh mai aatta tha ab to ghanta kuch samjh nhi aata

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

      @@akhilkumarsingh5041 In some videos he speaks too fast. On the digital pad, you do not know where he will start writing next - it is a bit disconcerting, and it also loses the human/personal touch. Plus, the topic takes a while to understand -- it may be helpful to go over the series one more time and take notes by pausing the video while he is talking. Earlier, it was easy when he summed up the video by going over the whole code, but now you don't have the whole code in front of you!

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

      @@0anant0 I can feel u bro. Now it feels like online classes

  • @rkalpeshk
    @rkalpeshk 4 роки тому +17

    void reverse(Stack stk ){
    if(stk.size()==1)
    return;
    int temp=stk.pop();
    reverse(stk);
    pushInStack(stk,temp);
    }
    void pushInStack(Stack stk, int temp){
    if(stk.size()==0){
    stk.push(temp);
    return;
    }else{
    int value=stk.pop();
    pushInStack(stk,temp);
    stk.push(value);
    return;
    }
    }

    • @0anant0
      @0anant0 4 роки тому +4

      Nice code! It would be even more easier to understand if you had renamed 'pushInStack' to something like 'push at bottom of stack' (bcoz push is normally done at top in a stack), also 'value' as 'top', temp = top for reverse(), temp = cur elem/key, etc. for the other fn. Just my rant... :-)

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

      Bro else is not needed in pushInStack function. Becoz u returned in the if condition. Just write whatever is inside the else but without else, it will work the same

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

      Time complexity =O(n^2)??

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

      code for reversing a stack and sort a stack are same?

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

      @@prakharnegi1081 yes except for the base condition where we compare temp value to top of stack in while sorting a stack.

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

    Thnx Bhaiya for such competitive coding..Helping me a lot in competitive coding

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

    Bhiya maine aapki sirf 2 video dekhi and I am able to solve questions myself till now lots of love 💫

  • @Vaishali-yt6qv
    @Vaishali-yt6qv 3 роки тому +1

    Bro You are God,Thank you very much for the Video;

  • @kushalappaca5324
    @kushalappaca5324 3 роки тому +1

    Best video on the internet....Recursion seems like cake walk indeed(until this part atleast)

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

    I don't really comment but like damn, i was compelled to. I was trying to figure out the recursive solution for like 10 mins and then thought to watch this video of yours to get a hint and oh boy, did I get one. Literally 4 minutes into the video, something clicked, i paused it and boom, solution accepted. A lot of ytbers cover these solutions but the ability to make these so intuitive is rarely observed. Thanks a ton man.
    P.S: Just realised you are the DP series guy :)

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

    Thankyou very much sir you made recursion cake walk!!

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

    Bhiaya aap super master ho ..pata nahi ab aap es chanel pr active ho ya nahi ....thank you so much ..the way you explain the question and concept it becomes like "chutny" ....you are khan sir for us

  • @UncoverIndia
    @UncoverIndia 4 роки тому +9

    static void reverse(Stack s){
    if(s.size()==1)
    return;
    int val=s.pop();
    reverse(s);
    swap(s,val);
    return;


    }
    static void swap(Stack s,int temp){
    if(s.size()==0){
    s.push(temp);
    return;
    }
    int val=s.pop();
    swap(s,temp);
    s.push(val);
    return;
    }

  • @priyanshupaliwal8548
    @priyanshupaliwal8548 5 місяців тому

    Thankyou so much, great explanation 👏👏👏👏

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

    Awesome style of teaching. Sir can u please upload series on graph theory. Please and thanks a lot for teaching us.

  • @Ashish.Kumar.Upadhyay
    @Ashish.Kumar.Upadhyay Рік тому

    Confused that recursion is magic or their is magic in your teaching. I have unlocked another view of recursion . Thanks. ❤❤

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

    Thanks brother now i am pretty comfortable in recursion✌🙂

  • @Ayush479
    @Ayush479 3 роки тому +1

    Nice explanation sir

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

    Sir I dis these question by myself tysm for Understanding the concept.

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

    Bhai
    Your videos are simply gold

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

    Finallllyyyy
    Mene code khud se likha
    Aur maja aya recursion padh ke

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

    This is fun!!! Thank you Aditya!

  • @umeshpatel6276
    @umeshpatel6276 3 роки тому +19

    #include
    using namespace std;
    void insert(stack &s, int temp)
    {
    // base condition
    if (s.size() == 0)
    {
    s.push(temp);
    return;
    }
    // hypothesis
    int x = s.top();
    s.pop();
    insert(s, temp);
    // induction
    s.push(x);
    }
    void reverse_Stack(stack &s)
    {
    // base condition
    if (s.size() == 1)
    {
    return;
    }
    // hypothesis
    int temp;
    temp = s.top();
    s.pop();
    reverse_Stack(s);
    // induction
    insert(s, temp);
    }
    int main()
    {
    stack s;
    int n;
    cout > n;
    for (int i = 0; i < n; i++)
    {
    int x;
    cin >> x;
    s.push(x);
    }
    reverse_Stack(s);
    for (int i = 0; i < n; i++)
    {
    cout

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

    Thankyou bhaiyaaa

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

    Instead of using a seperate stack and increasing auxilary space we used temp variable at each call frame which is also part of call stack at the end. this is one hell of a trick.

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

    The Game is Back........Thanks Bro .....Bhai Backtracking ki bhi video banaiye please....

  • @Vishal-ds6ly
    @Vishal-ds6ly Рік тому +1

    aap bahut acha padhate ho sir agar aap graph aur tree v padhaye hote to itne sare edtech startup na huye hote bhaiya

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

    Thank you bhaiya for such a great and simple explanation :)

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

    @Aditya Verma Yes....but we can't say that this algorithm take O(1) space. It's indeed works in O(n) space.

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

      Ignoring the internal call stack space bro*

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

      Okay, so, I have read in some Leetcode solution that, there are two types of solutions when it comes to space complexity:
      1. Constant Space solution: No recursive stack used. No extra data structure used. Nothing.
      2. In-place Solution: Here, we cannot explicitly take extra space using some data structure, but we can make use of recursion.
      And that's what he did here. The solution is in place as it doesn't allocate any extra space explicitly. But yes, it is not a constant space. And if it is about interviews, then, the interviewer is many times, pretty happy with such kind of intuitive solutions.

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

    Nicely explained

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

    sir u r best aapki apporach ache se smjh aati hai aap dry run krte ho ek ek ko phr aage code bhot sahi hai sir plzzzz sir dp graph bst tree sabki sari question batao

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

    Python solution:
    def insert_s(s,t):
    if len(s)==0:
    s.append(t)
    return
    temp=s.pop()
    insert_s(s,t)
    s.append(temp)
    def reverse_stack(s):
    if len(s)

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

    Bhai Graphs par bhi bano do ek playlist , bohot help hoga !! 🙏🙏

  • @nitinkumar5381
    @nitinkumar5381 3 роки тому +1

    i think base condition should be if(s.size()==0) then return; otherwise first element will not be reversed

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

    Auxillary space complexity is O(n) as mentioned.

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

    I have seen all your previous videos , pen and paper mode was better as you used to share a copy of the already written code to explain the discussed problem that was way better as the concepts and code was registered parallel. Your videos are amazing.

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

    Thank you sir ji

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

    Thank you bhya for uploading new videos 😊😊

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

    def reverse(stk,n,op):
    if n==0:
    return
    t=stk.pop()
    op.append(t)
    reverse(stk,n-1,op)
    return op
    stk=[1,2,3,4,5]
    op=[]
    n=len(stk)
    print(reverse(stk,n,op))

  • @Noone-kl6sc
    @Noone-kl6sc 3 роки тому

    Thank you bro... you are really awesome

  • @tusharkumar3277
    @tusharkumar3277 3 роки тому +1

    it will be very helpful if you post the question link also.

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

    Thanks for starting to make the videos again! Much awaited! Bas ek suggestion.
    Intro aur Outro music ki link har ek video description mein Credits kehke daal do bhai. YT fucks with honest creators every now n then, if there is copyright violation. Don’t want all this hard work to be taken down unnecessarily!

    • @TheAdityaVerma
      @TheAdityaVerma  4 роки тому +9

      Thanks for being so considerate brother !! But the music I used is "no copyright music" 😅 So cheers !!

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

      @@TheAdityaVerma Great work!! Keep em coming!

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

    AMAZING!! Can you please also explain reversing a linked list using recursion too!

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

    Python Solution-
    class Solving:
    def insert(self,arr,ele):
    if len(arr)==0:
    arr.append(ele)
    return
    temp=arr.pop()
    self.insert(arr,ele)
    arr.append(temp)
    def solve(self,arr):
    if len(arr)==1:
    return arr
    temp=arr.pop()
    self.solve(arr)
    self.insert(arr,temp)
    return arr
    lst=[1,2,3,4,5,6]
    obj=Solving()
    print(obj.solve(lst))
    Thanks @Aditya_Verma

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

    Great explanation bro 👍👏😊

  • @Shivamkumar-xu6hu
    @Shivamkumar-xu6hu 2 роки тому

    I liked your content that's why liked the video ... Subscribed the channel : )

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

    class Solution:
    def solve(self,arr,l,h):
    if l

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

    sir please make videos on backtacking and greedy approach...and nobody teaches like u...respect

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

    using iteration time- O(n) space- O(n)
    recursion time- O(n^2) space- O(1)

  • @ChandraShekhar-by3cd
    @ChandraShekhar-by3cd 4 роки тому +1

    Thanks for such a detailed explanation ...Which Digital pen and software you are using for drawing?? Please suggest I am planning to buy one for me.

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

    we can do this without insert function just by directly push(temp) in place of calling insert function as stack is lifo type , therefore when we push it goes to the bottom.

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

      No you cannot, if you push the element which you removed, it will get pushed back to the top itself, when you write the insert function, you are essentially popping all elements of a the stack and store them, push the temp element and push all other values on top of it.

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

    2 pointer and graph

  • @shikharsaxena9989
    @shikharsaxena9989 4 роки тому +7

    Recursion also takes stack memory so basically it is taking o(n) space ??

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

      Yes

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

      Thats what I have mentioned in the video, we are not allocating memory explicitly. That's the call stack memory getting allocated internally. So we can consider it to O(1).

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

    bhai trees pr vdos bna do plss!!

  • @KovidhVSBhati
    @KovidhVSBhati 8 місяців тому

    Jab pehli baar reverse function call karne pr reverse hogaya tha stack... toh recursion ki kya zarurat hai?

  • @lofireverbz-wy7go
    @lofireverbz-wy7go 9 місяців тому

    void insert(stack &s, int temp)
    {
    // base condition
    if (s.size() == 0)
    {
    s.push(temp);
    return;
    }
    // hypothesis
    int x = s.top();
    s.pop();
    insert(s, temp);
    // induction
    s.push(x);
    }
    void reverse_Stack(stack &s)
    {
    // base condition
    if (s.size() == 1)
    {
    return;
    }
    // hypothesis
    int temp;
    temp = s.top();
    s.pop();
    reverse_Stack(s);
    // induction
    insert(s, temp);
    }

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

    Kya padhaye ho sir ... Awesome breakdown of recursion

  • @kritartha886
    @kritartha886 26 днів тому

    element aur nikalke rkhne wala element dono same h ky?

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

    recursion is a magic

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

    thank you sir

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

    //Code For GFG-Reverse a Stack
    void pushElem(stack &s, int elem){
    if(s.size()==0){
    s.push(elem);
    return;
    }
    int val=s.top();
    s.pop();
    pushElem(s, elem);
    s.push(val);
    }
    void Reverse(stack &St){
    if(St.size()==1) return;
    int temp=St.top();
    St.pop();
    Reverse(St);
    pushElem(St, temp);
    return;
    }

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

    u r an angel!!!

  • @10iitg
    @10iitg 3 роки тому

    ye O(1) space nahi O(N) hi hai kyunki recursion mein har baar hum ek variable bana rahe hain temp jo total N baar banega

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

    legend of dp

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

    void reverse(stack s)
    {
    if(s.empty()) return;
    int top_element = s.top();
    s.pop();
    reverse(s);
    s.push(top_element);
    }
    Does this reverse in O(1) space ?

  • @saikeerthanchiluka4027
    @saikeerthanchiluka4027 9 місяців тому

    Super bro

  • @mdmehedihasan9999
    @mdmehedihasan9999 2 роки тому +3

    private static void reverse(Stack stack) {
    if (stack.size() == 0) {
    return;
    }
    Integer temp = stack.pop();
    reverse(stack);
    insert(stack, temp);
    }
    private static void insert(Stack stack, Integer temp) {
    if (stack.isEmpty()) {
    stack.push(temp);
    return;
    }
    Integer val = stack.pop();
    insert(stack, temp);
    stack.push(val);
    }