Next Greater Element | Animation | Coding Interview

Sdílet
Vložit
  • čas přidán 5. 11. 2021
  • ►Full DSA Course - • Data Structures and Al...
    ►Follow me on Instagram - bit.ly/intrvwkckstrt
    ►Follow me on LinkedIn - bit.ly/fllwlkdn
    ►Enroll in the complete course: bit.ly/3W4qthg
    ►Source Code - github.com/dinesh-varyani/ds-...
    ►Download DSA Animation Slides - techready.in/courses/150-dsa-...
    ►Click here to subscribe - czcams.com/users/hubbersp...
    Watch all my playlist here:
    ►Data Structures and Algorithms Course playlist: • Data Structures and Al...
    ►Mastering JUnit 5 - czcams.com/users/playlist?list...
    ►Mastering Mockito 3 - • Mockito 3 Tutorials
    ►Analysis of Algorithms - • Analysis of Algorithms
    ►Linked List Data Structures - • Linked List Data Struc...
    ►Array Data Structures - • Playlist
    ►Stack Data Structure - • Stack Data Structure
    ►Queue Data Structure - • Queue Data Structure
    ►Binary Tree Data Structure - • Binary Tree Data Struc...
    ►Graph Data Structure - • Graph Data Structure
    ►Binary Heap Data Structure - • Binary Heap Data Struc...
    ►Trie Data Structure - • Trie Data Structure
    ►Dynamic Programming Algorithms - • Dynamic Programming Al...
    ►Hashing Data Structures - • Hashing Data Structures
    ►Sorting and Searching - • Sorting and Searching
    ►String Algorithms - • String Algorithms
    Want to land a software engineering job in the IT industry? This course - 'Visualizing Data Structures and Algorithms' is here to help. The course walks you through multiple Java algorithms, data structures problems, and their solutions with step by step visualizations, so that you are actually learning instead of blindly memorizing solutions.
    The course covers in and outs of Data Structures and Algorithms in Java. Java is used as the programming language in the course. Students familiar with Javascript, Python, C#, C++, C, etc will also get to learn concepts without any difficulty. The implementation of various Algorithms and Data Structures have been demonstrated and implemented through animated slides. It covers many interview room questions on Algorithms and Data Structures. The questions and solutions are demonstrated by -
    1. Animated slide. (To make visualization of algorithms faster)
    2. Coding algorithm on IDE.
    The course covers topics such as -
    0. Algorithm Analysis
    1. Arrays
    2. Matrix
    3. Singly Linked List
    4. Doubly Linked List
    5. Circular Singly Linked List
    6. Stacks
    7. Queues
    8. Binary Tree
    9. Binary Search Tree
    10. Graphs
    11. Priority Queues and Heaps
    12. Recursion
    13. Searching
    14. Sorting
    15. Strings
    16. Trie Data Structure
    17. Dynamic Programming
    and many more ...
    #dsa #algorithms #coding
  • Jak na to + styl

Komentáře • 57

  • @bpriorb
    @bpriorb Před 2 lety +4

    Excellent work Dinesh describing the solution using a stack. I'm still struggling to fully understand the use of the stack without memorizing the solution. I think this technique can be used in other solutions as well. A "brute-force" two 'for' loop solution is easy, but this is much harder. Need to keep working on this...

    • @ajinxRGB
      @ajinxRGB Před 2 lety +3

      //yeah i tried the two for loop approach, that works as well
      int m = 0;
      int arr[] = { 4, 7, 3, 4, 8, 1 };
      int result[] = new int[arr.length];
      for (int i = 0; i < arr.length; i++) {
      for (int j = i + 1; j < arr.length; j++) {
      if (arr[i] < arr[j]) {
      result[m] = arr[j];
      break;
      }
      else {
      result[m] = -1;
      }
      }
      m++;
      }
      // for the last element
      result[arr.length-1]=-1;

  • @syedmohdadil4914
    @syedmohdadil4914 Před 2 lety +1

    What is time complexity for the solution?

  • @shubhamagarwal1434
    @shubhamagarwal1434 Před 2 lety +1

    Best series for DS & Algo. I am also 10 yrs java exp guy. Was looking for DS & Algo free course over CZcams with java implementation and found this. Hats Off To You Man...Excellent Work. GOD BLESS YOU :)

    • @itsdineshvaryani
      @itsdineshvaryani  Před 2 lety

      Thanks !!!

    • @ascar66
      @ascar66 Před rokem +2

      Hi 10 yrs java exp guy. I didn't see your comment on previous video I thought you gave up on this playlist. Glad to see you here again )))

    • @shubhamagarwal1434
      @shubhamagarwal1434 Před 9 měsíci

      @@ascar66 :)

  • @MohdDanish-kv8ry
    @MohdDanish-kv8ry Před 2 lety +4

    level is increasing. Very helpful. Thanks

  • @apsey2162
    @apsey2162 Před 2 lety +3

    Thanks a lot sir!!! . Keep uploading more videos like this.............

  • @AshishSingh-rx4sq
    @AshishSingh-rx4sq Před rokem +1

    Great visualization and explanation.

  • @032_gowthamkumarm_ecea9
    @032_gowthamkumarm_ecea9 Před 11 měsíci

    Hi, For [2,4,75,8,76,90,77,54] input should be passed in this code output should be wrong.

  • @albinkafexhiu9259
    @albinkafexhiu9259 Před rokem +1

    I think you should do Implementation to this video it is much better when we have both animation and implementation , BTW THANKS FOR ALL THE VIDEOS THIS IS BEST EXPLENATION I COULD FIND FOR ADS

  • @mrunaligaikwad1953
    @mrunaligaikwad1953 Před 2 lety +1

    Why did we not sort the array in ascending order ? or do we need to maintain the order of the nos?

    • @itsdineshvaryani
      @itsdineshvaryani  Před 2 lety +3

      If we sort the array, the order of elements will be change and we will not be able to find any elements next greater to right !!!

  • @ajose1070
    @ajose1070 Před 3 měsíci

    this metthod doesnt pass all test cases and had to use int i = 2* arr.length-1 and arr[i%n]

  • @poorpanda9033
    @poorpanda9033 Před 11 měsíci +1

    Thank You so much sir !

  • @AhsanAli-vz6mg
    @AhsanAli-vz6mg Před 2 lety +2

    Very well explained

  • @AmanPandey-or3dc
    @AmanPandey-or3dc Před 2 lety +3

    Sir I practice a lot on GFG, Hacker rank but still struggle to solve medium level problems and build logic. Please help. How should I prepare for fresher role interviews? Please Help?

    • @itsdineshvaryani
      @itsdineshvaryani  Před 2 lety +12

      Even I struggle to solve ... the idea still remains in practice ... practice and practice ... Be focused ... Be better than yesterday and soon after few months you will see change ... Dnt loose hope ... Keep solving problems ...

    • @AmanPandey-or3dc
      @AmanPandey-or3dc Před 2 lety +1

      @@itsdineshvaryani thanks sir surely i will

  • @bibekanandabesra6415
    @bibekanandabesra6415 Před 2 lety +1

    will the timing complexity be O(n*n)?? [cannot type superscript here]

    • @itsdineshvaryani
      @itsdineshvaryani  Před 2 lety

      Nope !!!

    • @bibekanandabesra6415
      @bibekanandabesra6415 Před 2 lety

      @@itsdineshvaryani how much then😬??

    • @bpriorb
      @bpriorb Před 2 lety

      I solved it using a "brute force" method first, using two 'for' loops, which is clearly O(n*n). Dinesh's way using a stack with a single 'for' loop is O(n) by my evaluation. It's a tradeoff of time vs space complexity. Please let me know if wrong. I've went thru Dinesh's solution a few times using the stack. It's quite tricky for me, and I'm still struggling how to think about it, without memorizing the solution.

  • @ERDheerajmann
    @ERDheerajmann Před 7 měsíci

    🙏

  • @karentechnologies3990
    @karentechnologies3990 Před 2 lety +1

    Using stack to solve this problem is hard, why don't we make things easy and use nested for loop?

  • @abdyashar5564
    @abdyashar5564 Před rokem +2

    Hello Sir. I'm following your DSA playlist consistently. Here when you described about the problem I'm just paused the video and I thought about the solution with pen and paper. after getting an idea I just implemented it to code. I solved this problem in such way and it still taking only one while loop while with two pointers, which is O(n) Time complexity. The output what i'm getting is correct.
    result is = [7, 8, 4, 8, -1, -1];
    The two pointers concept I've learned from your DSA course on youtube. This algorithm also giving correct result with O(n) TC
    what is your opinion about this algo, kindly correct me if I'm wrong.
    public int[] nextGreaterElement(int[] arr) {
    int[] result = new int[arr.length];
    //two pointer
    int i = 0;
    int j = i + 1;
    int k = arr.length - 1;
    int notExists = -1;
    while (i < arr.length) {
    // edge cases
    if (j >= arr.length)
    {
    result[i] = notExists;
    break;
    }
    if (arr[i] > arr[j] && j == k)
    {
    result[i] = notExists;
    i++;
    j = i + 1;
    continue;
    }
    //assigning the next greater element to the result array
    if (arr[i] < arr[j])
    {
    result[i] = arr[j];
    i++;
    j = i + 1;
    }
    else
    {
    j++;
    }
    }
    return result;
    }

    • @pradeeppradhi8792
      @pradeeppradhi8792 Před rokem +1

      I was actually thinking about the same code but in edge cases i was doing mistake so thanks for the code

    • @sumitpal6797
      @sumitpal6797 Před rokem

      bro its not O(n)..... calculate the complexity of this :- [8,7,6,5,4,3,2,1,9]

  • @Jayantch.999
    @Jayantch.999 Před rokem +1

    super explanation

  • @sadiulhakim7814
    @sadiulhakim7814 Před rokem

    Hello, sir. I tried this algorithm at myself......
    My Solution..
    public int[] nextGreater(int[] arr){
    int[] result=new int[arr.length];

    for(int i=0;i

  • @shahidulhasan1147
    @shahidulhasan1147 Před 2 lety +1

    Sir i have the same question. Till where should we think that now It's time for us to stop dsa and learn development?

    • @itsdineshvaryani
      @itsdineshvaryani  Před 2 lety +5

      look knowledge is never measured by time ... you cannot stop dsa at any point and learn development or vice versa ... i hv more than 11 years of exp but i still struggle to solve leetcode questions ... but am happy with what i knw .... its a journey dnt count it on time .... you will realize this when u will hv exp ... my words will sound correct .... practice both and dnt stop anything to learn other ... do both simultaneously ...

    • @shahidulhasan1147
      @shahidulhasan1147 Před 2 lety +2

      Thanks sir.i got it.you're great

    • @itsdineshvaryani
      @itsdineshvaryani  Před 2 lety +2

      thanks !!! keep supporting !!!

  • @VivekSingh-ms3vu
    @VivekSingh-ms3vu Před 2 lety +2

    Please upload operating system videos in java

  • @rahuldubey9999
    @rahuldubey9999 Před 2 lety +1

    hi
    i tried with this input value int arr[] = {2, 8, 9, 6, 7}; and getting the wrong output as {8 9 -1 7 -1} . I think it should be { 6, 9, -1, 7, -1}
    can you please check and help me. what I am doing wrong. i pasted the whole code below.
    int arr[] = {2, 8, 9, 6, 7};
    int result[] = new int[arr.length];
    Stack stack = new Stack();
    for ( int i = arr.length-1; i >=0; i-- ) {

    if(!stack.isEmpty()) {
    while(!stack.isEmpty() && stack.peek() System.out.print(a+" "));

    • @yuva0101
      @yuva0101 Před 2 lety +2

      Output is correct boss!!
      I think you understood the problem wrong.
      For 2-8, for 8-9, for 9- -1, for 6-7, and for 7 - -1.

    • @rahuldubey9999
      @rahuldubey9999 Před 2 lety

      @@yuva0101 Thanks Yuvasai... I am new to the coding...learning steps by steps

    • @itsdineshvaryani
      @itsdineshvaryani  Před 2 lety +2

      Output is correct !!! Its next greater element towards right !!!

  • @pentainamerica
    @pentainamerica Před 2 lety +1

    we can solve this by using 2 for loops and just arrays right.....without using stacks..

  • @raghavmanchanda4736
    @raghavmanchanda4736 Před 2 lety +2

    Sir by much time will this course be completed