Next Greater Element | Animation | Coding Interview

  Рет қаралды 17,583

Dinesh Varyani

Dinesh Varyani

Күн бұрын

Пікірлер: 58
@shubhamagarwal1434
@shubhamagarwal1434 2 жыл бұрын
Best series for DS & Algo. I am also 10 yrs java exp guy. Was looking for DS & Algo free course over KZbin with java implementation and found this. Hats Off To You Man...Excellent Work. GOD BLESS YOU :)
@itsdineshvaryani
@itsdineshvaryani 2 жыл бұрын
Thanks !!!
@ascar66
@ascar66 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 Жыл бұрын
@@ascar66 :)
@MohdDanish-kv8ry
@MohdDanish-kv8ry 3 жыл бұрын
level is increasing. Very helpful. Thanks
@itsdineshvaryani
@itsdineshvaryani 3 жыл бұрын
welcome !!!
@bpriorb
@bpriorb 2 жыл бұрын
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 2 жыл бұрын
//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;
@apsey2162
@apsey2162 2 жыл бұрын
Thanks a lot sir!!! . Keep uploading more videos like this.............
@itsdineshvaryani
@itsdineshvaryani 2 жыл бұрын
Sure !!!
@AshishSingh-rx4sq
@AshishSingh-rx4sq 2 жыл бұрын
Great visualization and explanation.
@itsdineshvaryani
@itsdineshvaryani 2 жыл бұрын
Thanks !!!
@AdilSyed_Imam
@AdilSyed_Imam 3 жыл бұрын
What is time complexity for the solution?
@albinkafexhiu9259
@albinkafexhiu9259 Жыл бұрын
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
@itsdineshvaryani
@itsdineshvaryani Жыл бұрын
Welcome
@mrunaligaikwad1953
@mrunaligaikwad1953 2 жыл бұрын
Why did we not sort the array in ascending order ? or do we need to maintain the order of the nos?
@itsdineshvaryani
@itsdineshvaryani 2 жыл бұрын
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 !!!
@AmanPandey-or3dc
@AmanPandey-or3dc 2 жыл бұрын
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 2 жыл бұрын
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 2 жыл бұрын
@@itsdineshvaryani thanks sir surely i will
@bibekanandabesra6415
@bibekanandabesra6415 2 жыл бұрын
will the timing complexity be O(n*n)?? [cannot type superscript here]
@itsdineshvaryani
@itsdineshvaryani 2 жыл бұрын
Nope !!!
@bibekanandabesra6415
@bibekanandabesra6415 2 жыл бұрын
@@itsdineshvaryani how much then😬??
@bpriorb
@bpriorb 2 жыл бұрын
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.
@poorpanda9033
@poorpanda9033 Жыл бұрын
Thank You so much sir !
@raghavmanchanda4736
@raghavmanchanda4736 3 жыл бұрын
Sir by much time will this course be completed
@itsdineshvaryani
@itsdineshvaryani 3 жыл бұрын
I will keep adding to this course ... DSA is very vast and never ending topic ...
@raghavmanchanda4736
@raghavmanchanda4736 3 жыл бұрын
@@itsdineshvaryani ok sir for sure
@abdyashar5564
@abdyashar5564 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 2 жыл бұрын
I was actually thinking about the same code but in edge cases i was doing mistake so thanks for the code
@sumitpal6797
@sumitpal6797 2 жыл бұрын
bro its not O(n)..... calculate the complexity of this :- [8,7,6,5,4,3,2,1,9]
@VivekSingh-ms3vu
@VivekSingh-ms3vu 3 жыл бұрын
Please upload operating system videos in java
@itsdineshvaryani
@itsdineshvaryani 3 жыл бұрын
operating system videos in Java ????
@VivekSingh-ms3vu
@VivekSingh-ms3vu 3 жыл бұрын
@@itsdineshvaryani various algorithm as well as codes like Peterson’s, dekker,etc
@itsdineshvaryani
@itsdineshvaryani 3 жыл бұрын
ok
@AhsanAli-vz6mg
@AhsanAli-vz6mg 3 жыл бұрын
Very well explained
@itsdineshvaryani
@itsdineshvaryani 3 жыл бұрын
thanks !!!
@032_gowthamkumarm_ecea9
@032_gowthamkumarm_ecea9 Жыл бұрын
Hi, For [2,4,75,8,76,90,77,54] input should be passed in this code output should be wrong.
@dineshr4732
@dineshr4732 19 күн бұрын
public static int[] findNextGreaterElements(int[] nums) { int n = nums.length; int[] result = new int[n]; Stack stack = new Stack(); // Traverse the array from left to right for (int i = 0; i < n; i++) { // Check for elements that have a greater next element while (!stack.isEmpty() && nums[stack.peek()] < nums[i]) { int idx = stack.pop(); result[idx] = nums[i]; } // Push the current index onto the stack stack.push(i); } // For elements in the stack, there is no next greater element, so set them to -1 while (!stack.isEmpty()) { result[stack.pop()] = -1; } return result; }
@rahuldubey9999
@rahuldubey9999 2 жыл бұрын
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 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 2 жыл бұрын
@@yuva0101 Thanks Yuvasai... I am new to the coding...learning steps by steps
@itsdineshvaryani
@itsdineshvaryani 2 жыл бұрын
Output is correct !!! Its next greater element towards right !!!
@ajose1070
@ajose1070 8 ай бұрын
this metthod doesnt pass all test cases and had to use int i = 2* arr.length-1 and arr[i%n]
@shahidulhasan1147
@shahidulhasan1147 3 жыл бұрын
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 3 жыл бұрын
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 3 жыл бұрын
Thanks sir.i got it.you're great
@itsdineshvaryani
@itsdineshvaryani 3 жыл бұрын
thanks !!! keep supporting !!!
@Jayantch.999
@Jayantch.999 2 жыл бұрын
super explanation
@itsdineshvaryani
@itsdineshvaryani 2 жыл бұрын
thanks
@karentechnologies3990
@karentechnologies3990 2 жыл бұрын
Using stack to solve this problem is hard, why don't we make things easy and use nested for loop?
@itsdineshvaryani
@itsdineshvaryani 2 жыл бұрын
TC will increase ...
@pentainamerica
@pentainamerica 2 жыл бұрын
we can solve this by using 2 for loops and just arrays right.....without using stacks..
@itsdineshvaryani
@itsdineshvaryani 2 жыл бұрын
nested for loops will increase the TC
@sadiulhakim7814
@sadiulhakim7814 2 жыл бұрын
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
@ERDheerajmann
@ERDheerajmann Жыл бұрын
🙏
Product of an Array except self | Animation | Coding Interview
22:25
Dinesh Varyani
Рет қаралды 4,7 М.
Why Floyd's Cycle Detection algorithm works?
19:30
Dinesh Varyani
Рет қаралды 21 М.
So Cute 🥰 who is better?
00:15
dednahype
Рет қаралды 19 МЛН
coco在求救? #小丑 #天使 #shorts
00:29
好人小丑
Рет қаралды 114 МЛН
Next Greater Element | Two Variants | Leetcode
22:00
take U forward
Рет қаралды 258 М.
MY ULTIMATE LEETCODE TRICKS
8:12
PIRATE KING
Рет қаралды 257 М.
Making an Algorithm Faster
30:08
NeetCodeIO
Рет қаралды 172 М.
Iterative Inorder traversal of a Binary Tree in Java
28:43
Dinesh Varyani
Рет қаралды 22 М.
LeetCode Next Greater Element I Solution Explained - Java
9:51
Nick White
Рет қаралды 34 М.
How to move Zeroes to end of an Array? | Animation
14:45
Dinesh Varyani
Рет қаралды 41 М.
Recursive Inorder traversal of Binary Tree in Java
26:57
Dinesh Varyani
Рет қаралды 43 М.
Mastering Dynamic Programming - How to solve any interview problem (Part 1)
19:41