Next Greater Element | GeeksforGeeks

Sdílet
Vložit
  • čas přidán 29. 10. 2016
  • Explanation for the article: www.geeksforgeeks.org/next-gre...
    Read More: www.geeksforgeeks.org/next-gr...
    This video is contributed by Harshit Jain.

Komentáře • 36

  • @pankajrathi
    @pankajrathi Před 3 lety +3

    One thing I can say is GeeksforGeeks is not making the videos to the expectations people have. I see a lot of videos in this playlist where either the instructor is just dry running or reading out the solution than explaining the intuition /reason behind it

  • @vasumavani1280
    @vasumavani1280 Před 4 lety +4

    What if, our output must be in same order or sequence as that of input array, then how indexing will be done?

  • @mihailazar2487
    @mihailazar2487 Před 6 lety +3

    one question ... WHY do you need to push elements out of the stack to compare them ? you can use can't u use s.top() ?

  • @Roych0325
    @Roych0325 Před 6 lety +2

    the pseudo code of method 2 doesn't mention about pushing next and when to do it.

  • @DoubleCoolOp
    @DoubleCoolOp Před 6 lety

    What if you solve this problem using queue instead of stack ?

  • @MsNareshJoshi
    @MsNareshJoshi Před 7 lety +3

    I have doubt if that if my array is 100, 99, 98, 97, ......, 50, 121, 49, 48, 47, ......., 3, 2, 1, 120 then how it is still O(n)?

  • @alexanderyakovlev9311
    @alexanderyakovlev9311 Před 4 lety

    The pseudocode for Step 2, sub-step 5 is wrong, regarding "then push the popped element back". It should be "then push next into the stack". So basically the stack is used to keep all the scanned elements whose GNE are yet unknown.

  • @-arabsoccer1553
    @-arabsoccer1553 Před 5 lety

    i think the complexity of the 2nd approach is only O(2n)
    because at worst case if all element is in decreasing order like for example:15,13,11,3
    it will push all of them to stack because condition (element < next) is not correct
    so the stack will be fall ,then the finally while loop will pop the element from stack and set the next to -1 for all elements.

  • @rajdeepbadole2183
    @rajdeepbadole2183 Před 4 lety

    Not working for 7 8 1 4 (using Stack)

  • @kevinpatel6157
    @kevinpatel6157 Před 6 lety +2

    Instead we can start finding NGE from end of array using stack just like previous video in this tutorial
    Its quite simple than that given in video

    • @yazanshakhshir3049
      @yazanshakhshir3049 Před 5 lety +1

      indeed my friend.
      I thought just the same way as you thought ;)
      it more efficient and easier

  • @susheelkumarbhargav
    @susheelkumarbhargav Před 3 lety

    why can't we iterate from the end of the array?

  • @eveo5055
    @eveo5055 Před 4 lety

    problem implementation using stack at 12:40 (skipped basic stack operation)

  • @fabscs-wy3hp
    @fabscs-wy3hp Před 7 lety

    doesn't the second method require more space?

    • @spacesuitred3839
      @spacesuitred3839 Před 6 lety

      I think so because in 2nd algorithm we are using linked list to create stack which consumes more space than an array in 1st algorithm

  • @bendakai1
    @bendakai1 Před 4 lety +1

    i think we can do it using arrays also where each element stores the index of the next greatest element. we can fill this array from right to left. time complexity is O(n) and space complexity also would be O(n)

    • @harshjoshi6257
      @harshjoshi6257 Před 4 lety

      i dont think so.....

    • @bendakai1
      @bendakai1 Před 4 lety +1

      @harish Joshi. You are correct. We can't do using array. I was in different understanding of problem statement earlier.

  • @vineetkothari2839
    @vineetkothari2839 Před 7 lety

    nice article

  • @jayeshkandpal7860
    @jayeshkandpal7860 Před 4 lety

    Why do i feel like it's harkirat explaining this video

  • @ch_dktomar
    @ch_dktomar Před 7 lety +1

    IF we have elements as 4, 8, 6,25 it says(in stack method) push 4 in stack and them compare 8 to 4 if 8 is grater then it is the NGE for 4 but this is wrong the NGE for 4 is 6. Am I thinking correct or plz someone correct me....

    • @GeeksforGeeksVideos
      @GeeksforGeeksVideos  Před 7 lety +4

      The problem statement states that the Next greater Element for an element x is the first greater element on the right side of x in array.
      Hope this solves your confusion. :)

    • @ch_dktomar
      @ch_dktomar Před 7 lety

      GeeksforGeeks yes thanks I got it...

    • @ch_dktomar
      @ch_dktomar Před 7 lety

      yes thsnks...

  • @albertdavidson1373
    @albertdavidson1373 Před 3 lety

    It seems Shushant Singh Rajput is explaining !!

  • @kamalghai92
    @kamalghai92 Před 2 lety

    We can peek() top and check if top is less than next. We won't need to push back again in this case. My java code -
    private static void printNGE(Stack stack, int arr[]) {
    if(arr == null) return;

    stack.push(arr[0]);

    for(int i = 1; i < arr.length; i++) {
    int next = arr[i];

    if(!stack.isEmpty() && stack.peek().getData() < next) {
    int element = stack.pop().getData();
    while(element < next) {
    System.out.println("NGE for " + element + " is " + next);
    if(stack.isEmpty()) break;
    element = stack.pop().getData();
    }
    }

    stack.push(next);
    }
    while(!stack.isEmpty()) {
    System.out.println("NGE for " + stack.pop().getData() + " is -1");
    }

  • @yoursNataraj
    @yoursNataraj Před 4 lety

    Here is the solution :
    czcams.com/users/timedtext_cs_panel?c=UCninsT8lEtwlJYedzYY7bnA&tab=2

  • @AbhijitGaikwad-gabhi
    @AbhijitGaikwad-gabhi Před 7 lety

    Java version of the code
    public static void getNGE(int[] a) {
    Stack s = new Stack();
    s.push(a[0]);
    for (int i = 1; i < a.length; i++) {
    if (s.peek() != null) {
    while (true) {
    if (s.peek() == null || s.peek() > a[i]) {
    break;
    }
    System.out.println(s.pop() + ":" + a[i]);
    }
    }
    s.push(a[i]);
    }
    while (s.peek() != null) {
    System.out.println(s.pop() + ":" + -1);
    }
    }

    • @GeeksforGeeksVideos
      @GeeksforGeeksVideos  Před 7 lety

      Thank you, Abhijit! :)
      I've shared this with our content team.

    • @MsNareshJoshi
      @MsNareshJoshi Před 7 lety

      We should also make check for empty stack using s.isEmpty(), so code should be
      public static void getNGE(int[] a) {
      Stack s = new Stack();
      s.push(a[0]);
      for (int i = 1; i < a.length; i++) {
      if (s.peek() != null) {
      while (true) {
      if (s.isEmpty() || s.peek() == null || s.peek() > a[i]) {
      break;
      }
      System.out.println(s.pop() + ":" + a[i]);
      }
      }
      s.push(a[i]);
      }
      while (!s.isEmpty() && s.peek() != null) {
      System.out.println(s.pop() + ":" + -1);
      }
      }

  • @madhukarsain1895
    @madhukarsain1895 Před 4 lety +5

    way of explanation is not good

  • @mihailazar2487
    @mihailazar2487 Před 6 lety

    Goddammit, man that accent is painful to listen to

  • @siva.....45
    @siva.....45 Před rokem

    find the next greatest element for each element in the array
    input 8 5 9 2 3
    output 9 8 -1 3 5