2 NGR | Nearest Greater to right | Next Largest Element

  Рет қаралды 302,577

Aditya Verma

Aditya Verma

Күн бұрын

Пікірлер: 483
@kshmaverma5688
@kshmaverma5688 4 жыл бұрын
Bhaiya aap jaise 1-2 utubers aur aa jaye..fir coding blocks aur ninjas to katora lekrr ghumenge.... U r a big support for the students like me who cannot afford those extremely expensive courses. Request h ki aap continue krtee rhoo... Plzz.... 🤣
@thexhist3609
@thexhist3609 3 жыл бұрын
We also have pepcoding which is good and everything is free.
@tanned_cosines_
@tanned_cosines_ 2 жыл бұрын
lmfaoooooo
@mishorpatra1670
@mishorpatra1670 3 жыл бұрын
"Once you learn to approach problems, you'll stop running after the solution." - Sir Aditya Verma (bas ab ek backracking aur graph playlist banado aur kuch nahi chahiye)
@amarnathprasad1
@amarnathprasad1 3 жыл бұрын
Until he makes one, you can watch the first six lectures from here and solve questions from the FreeCodeCamp video(7th video in the playlist). I got better at graphs after practicing questions from the FCC video. kzbin.info/aero/PLmqutD5ta9PvsMYCyc0tVpmIl8-iIgxey
@nitwaalebhaiya
@nitwaalebhaiya 3 жыл бұрын
@@amarnathprasad1 Thanks
@Adarsh-mn7pl
@Adarsh-mn7pl 2 жыл бұрын
bul ja, ab kuch nhi ane wala
@nidhishprasad2506
@nidhishprasad2506 2 жыл бұрын
@@Adarsh-mn7pl why?
@mantenapragnyabala1711
@mantenapragnyabala1711 4 ай бұрын
​Hi do you know from where to do queue​@@amarnathprasad1
@govindsharma6951
@govindsharma6951 7 ай бұрын
can not explain how much happy i am after finding your channel. thank u so much for creating these videos
@hereyoursdad
@hereyoursdad 2 жыл бұрын
sir more then that the little talk from your exeperience like what they want from us is really a diamond .. thank you so much for making these gems will help so many peoples
@upasanaarora2641
@upasanaarora2641 4 жыл бұрын
for almost 1 month searching for new fresh content to revise my concepts again,loved your channel.Its has all,what it needs to be done even in short time,To the point and great content.Great Job :)
@Area-fd8ht
@Area-fd8ht Жыл бұрын
@karanbhati7552
@karanbhati7552 3 жыл бұрын
More Concise code :: vector nextLargerElement(vector a, int n){ stack s; vector ans; for(int i=n-1;i>=0;i--){ while(!s.empty() && a[i]>=s.top()){ s.pop(); } if(s.empty()) ans.push_back(-1); else ans.push_back(s.top()); s.push(a[i]); } reverse(ans.begin(),ans.end()); return ans; }
@pragyanshandilya4015
@pragyanshandilya4015 2 жыл бұрын
legend
@charugoswami5344
@charugoswami5344 Жыл бұрын
Kindly help me to find error in this code.... vector nextGreaterElements(vector& nums) { stacks; vectorv; for(int i=nums.size()-1;i>=0;i--){ while(!s.empty() && s.top()
@_AmbujJaiswal
@_AmbujJaiswal Жыл бұрын
bhati bhai thankyou soo much
@samikshayadav3475
@samikshayadav3475 Жыл бұрын
good good
@manjeshpatil
@manjeshpatil Жыл бұрын
So basically at n-1 checking your code executes the while loop first even though the stack is empty that's y write the if condition 1st then else then the while loop. Correct me if iam wrong @@charugoswami5344
@comps_52_mankritsingh84
@comps_52_mankritsingh84 3 жыл бұрын
*Interesting thing to note* For those who are getting segmentation error even though your code is the same check whether you have done the seek or top operation on the stack in your code? for eg in the code while(s.top()0) In this code when the stack is empty the first condition will be checked first and give you segmentation error.Instead, keep the statement after the empty check and the error will go. Hope it helps ;)
@abhijeeyt
@abhijeeyt 2 жыл бұрын
bhai 🙏🙏
@Lucifer-jl9vh
@Lucifer-jl9vh 2 жыл бұрын
Thank you bhai
@pranjalmukherjee
@pranjalmukherjee Жыл бұрын
Thank you bhai
@tanya2586
@tanya2586 Жыл бұрын
heyy.. i'm still getting error.. can you check why? vector v; stack s; for(int i=n-1;i>=0;i--){ if(s.empty()){ v.push_back(-1); } else if(s.top()>arr[i]){ v.push_back(s.top()); } else if(arr[i]>=s.top()){ while( s.size()>0 && arr[i]
@comps_52_mankritsingh84
@comps_52_mankritsingh84 Жыл бұрын
@@tanya2586 try by removing that ! Sign in the last check you have put if(!s.empty()) .
@abhijeetgupta3461
@abhijeetgupta3461 4 жыл бұрын
reverse(v.begin(),v.end()) after for loop ...
@ShahNawaz-cx3pi
@ShahNawaz-cx3pi 3 жыл бұрын
*********Time complexity Doubt:-******** I got what you want to ask:- your main concern is about the inside while() loop, you are thinking that it will become O(n^2) , but this will never happen the worst case complexity can be O(2n) , in simple terms O(n) . Case 1 :- arr [] :- 6 5 4 3 2 1 In this case the while() loop will run only one time in each iteration of for() loop so, while() loop is contributing only O(1) complexity. In this case it is simple O(n). Case 2:- are[]:- 6 1 2 3 4 5 In this case every element in the array will considered 2 times. First time when we are traversing the array in backward direction through for() loop and another time when we traverse element in forward direction through while() loop. It is just like traversing array two times O(n) +O(n) =O(2*n). So, The overall complexity become O(2n) ~ O(n).
@tiyashadas5247
@tiyashadas5247 3 жыл бұрын
Thank you!
@sanyam9712
@sanyam9712 2 жыл бұрын
Thankyou
@kingrajput6202
@kingrajput6202 2 жыл бұрын
thanks man
@PankajSingh-vi3fy
@PankajSingh-vi3fy 2 жыл бұрын
For me it's kinda tricky but got your point thanks
@rajatagarwal6091
@rajatagarwal6091 2 жыл бұрын
Hi @Shah Nawaz. Thanks for your explanation. However I still have a doubt that worst case complexity would be O(n^2). let consider some array like 20,14,15,16,17,18,19, 10,7,8,9 , 6,1,2,3,4,5. What are your thoughts on this array?
@williamgilfoyle3261
@williamgilfoyle3261 2 жыл бұрын
Best explanation so far, but i would prefer "while loop" over "for loop" , for current problem and in the third if case we dont move to the next element in the array until we pop all the elements from array Like here Python : Language arr = [1,3,0,0,1,2,4] stack = [ ] res = [ ] i = len( arr ) - 1 while i>=0: if not stack: res.append(-1) elif stack[ -1 ]>arr[ i ]: res.append(stack[-1]) elif stack[ -1 ]
@sushmitamandal1677
@sushmitamandal1677 Жыл бұрын
my programme is not running can you please help to run the code?please
@pratikkedar4476
@pratikkedar4476 2 жыл бұрын
Small Optimization : We can avoid using reverse method, just need to push elements from array in reverse order ! Make sure you define fixed vector length else will give compilation errors!!
@pokeindia5361
@pokeindia5361 Жыл бұрын
Why is this so we don't need size of vectors ryt?? I'm facing this problem
@Demodulator7
@Demodulator7 Жыл бұрын
​@@pokeindia5361 we need size of answer vector and it will be equal to given vector
@pokeindia5361
@pokeindia5361 Жыл бұрын
@@Demodulator7 I'm asking why we need to fixed vector length vector is automatically adjusted on its own ryt??
@Demodulator7
@Demodulator7 Жыл бұрын
@@pokeindia5361 but in optimization, you are filling vector from backward (so that you not need to reverse it) so you have to give the length that from where you are filling. And you can't use push_back because it adds value in last, but each time you want to add values at front because first you calculated value for I = n-1 (last element) and then for i= n-2 (second last element) and so on till the first element. So you have to do V[i]= .... So you have to give vector a size because vector can increase it's size in one direction only. If you directly do V[n-1] =.... Then it will give runtime error because vector is trying to access n-1 position but currently vector size is 0. One more point:- doing push_back doesn't require size declaration. But when ever you do V[I] = .... Then size of vector should be greater then I. If not want to declare size then you can use 'list' and do push_front Hope it helps...
@pokeindia5361
@pokeindia5361 Жыл бұрын
@@Demodulator7 thank u so much sir!!
@adarshanand176
@adarshanand176 4 жыл бұрын
this is ur first impression on me... and its really looks like my dream teaching style. 🤩✌
@abhishekojha4540
@abhishekojha4540 2 жыл бұрын
brother I was stuck in this question for so long time and no one has explained this question to me in so much detail. Thanks a lot.
@adityachauhan9951
@adityachauhan9951 8 ай бұрын
best explanation available on KZbin thank you so much
@SauravKumar-si7kt
@SauravKumar-si7kt 3 жыл бұрын
This is sth which i need in other youtube solutions. I never got satisfied with their solution because they are just discussing the solution. And those solution never helped me. Thank you for introducing these all concept. It would have taken a lot of time to think on our own.
@akashdawari4665
@akashdawari4665 4 жыл бұрын
continue bro, this good work ;)
@TheAdityaVerma
@TheAdityaVerma 4 жыл бұрын
Thanks brother, Do subscribe and share, that keeps me motivated to do more !!
@samirsaurabh5785
@samirsaurabh5785 3 жыл бұрын
@@TheAdityaVerma please make such great videos on all data structures and algortihms,you are the true mentor ❤️.
@nandasaikishore478
@nandasaikishore478 3 жыл бұрын
@@TheAdityaVerma please continue the course sir please 🙏🙏🙏
@souravsaha3446
@souravsaha3446 3 жыл бұрын
@@TheAdityaVerma Brother please upload videos on some other topics please You are not only a teacher to me but also a coding guru who can teach with his experience please sir upload on backtracking and graphs
@vathsan3906
@vathsan3906 3 жыл бұрын
Bhai.. Sorry I would like to call you SIR.. Hats off.. I was scratching my head for the past 2 days trying to figure out why they are using stack, how to identify what to use and when to use and also to get the approach to solve... I was unable to understand it from GFG and thank god I found you!! Finally understood why we are using stack and what is it's purpose in this question...
@RohithRock
@RohithRock 4 жыл бұрын
Crystal clear explanation bro. Really it helped me a lot in understanding the concepts perfectly. I was really afraid of datastructures and algorithms. I was not able to solve the questions thinking that ds is a really tuff subject. But now through ur way of teaching and explaining i am able to learn and understand easily. Thank you bro. Do more videos regarding the ds topics. From - rohith Nit bhopal cse
@hackzyoboy2459
@hackzyoboy2459 4 жыл бұрын
165th like, 0 dislikes. That's the power of generous teaching. Great job bhaii
@amankumar-im8kk
@amankumar-im8kk 4 жыл бұрын
Awesome content. You will be hitting 1M subscribers soon. Far better than paid contents of coding blocks and coding ninjas.
@theunusual4566
@theunusual4566 2 жыл бұрын
Matlab, Kamaal Ka explanation bhai. Automatically understood next 3 questions. Bahut Khood Samjhaya Aapne. Thanks.
@animeshsingh2880
@animeshsingh2880 3 жыл бұрын
Best videos in youtube for learning DSA
@utsavseth6573
@utsavseth6573 Жыл бұрын
Not all heroes wear capes. Some are named aditya verma. Superb.
@jhanvisaraswat6976
@jhanvisaraswat6976 3 жыл бұрын
Those three conditions putting things in perspective so much
@akarshjaiswal1622
@akarshjaiswal1622 4 жыл бұрын
Great explanation... Please start uploading videos again... Looking forward for mathematics, backtracking,graph videos !
@malkeetsapien4852
@malkeetsapien4852 2 жыл бұрын
Brilliantly taught Sensei !
@NotNotNithin
@NotNotNithin 3 жыл бұрын
Java implementation: private static List nearestGreatestToRight(int[] arr) { Stack st = new Stack(); List reverseList = new ArrayList(); List resList = new ArrayList(); int size = arr.length; for (int i = size - 1; i >= 0; i--) { if (st.empty()) { reverseList.add(-1); } else if (st.size() > 0 && st.peek() > arr[i]) { reverseList.add(st.peek()); } else if (st.size() > 0 && st.peek()
@ayush7518
@ayush7518 2 жыл бұрын
thanks a lot bhai
@salonipaliwal8946
@salonipaliwal8946 Жыл бұрын
thank you
@RohitKumar-uc4fw
@RohitKumar-uc4fw Жыл бұрын
Use this code to directly store from right to left. public static int[] findNextGreater(int[] ar) { int[] result = new int[ar.length]; Stack stack = new Stack(); for(int i = 0; i < ar.length; i++) { while(!stack.isEmpty() && ar[i] > ar[stack.peek()]) { int index = stack.pop(); result[index] = ar[i]; } stack.push(i); } while(!stack.isEmpty()) { int index = stack.pop(); result[index] = -1; } return result; }
@shivagupta138
@shivagupta138 10 ай бұрын
@@RohitKumar-uc4fw use this and you wont need to reverse it even " public static int[] nearestright(int []arr) { Stack stack=new Stack(); int result[]=new int[arr.length]; for(int i=arr.length-1; i>=0; i--) { if(stack.size()==0) { result[i]=-1; } if(stack.size()>0 && stack.peek()>arr[i]) { result[i]=stack.peek(); } if(stack.size()>0 && stack.peek()0 && stack.peek()
@abhishekanantharam645
@abhishekanantharam645 3 жыл бұрын
O bhai legend aadmi hai ye toh. Thanks✌️
@vinayakrastogi890
@vinayakrastogi890 4 жыл бұрын
Bro I am waiting for your backtracking playlist... I know you have a busy schedule but please make videos on that
@shubhamjain54519
@shubhamjain54519 2 жыл бұрын
14:18 for context. 15:19 2 can be safely popped because we have already found an element on its left which is greater than 2, ie 3. As any element (greater than 2 and smaller than 3) on left of 3 which needs next greater element on its right will always print 3, so 2 is of no use at this point.
@alexamish2020
@alexamish2020 4 жыл бұрын
Ye channel bhot zyada grow krege. Gauranteed.
@nirbhay411
@nirbhay411 4 жыл бұрын
Bhai kya hi samjhaya he apne . maja agaya
@aheribanerji7056
@aheribanerji7056 3 жыл бұрын
Pls, sir make more videos... it's very helpful for students like us who can't afford the high prized course .
@harjos78
@harjos78 4 жыл бұрын
loved watching your videos.. Awesome .. crystal clear explaination. Was refreshing my DS concepts to teach my son who is in 7th grade. Now after he has watched your videos he says your explaination is way better than i can explain him.. Kudos Aditya. Keep up the good work. God bless you
@ankitsharda1131
@ankitsharda1131 2 жыл бұрын
With due respect to you I would suggest you to let your son enjoy his childhood. All this he can learn it when it will be the right time.
@vanshshah7781
@vanshshah7781 Жыл бұрын
7th grade is too late man
@rushikesh_chaudhari
@rushikesh_chaudhari 4 жыл бұрын
Just started watching your playlist and this is really mind blowing. Loved your way to find solution for problems
@AyushGupta-ux4gq
@AyushGupta-ux4gq 11 ай бұрын
what are you doing now after 3 years of this comment ??
@bestsaurabh
@bestsaurabh 4 жыл бұрын
Instead of taking vector we can take array start filling it from end, this way we can save reverse function call.
@aayush5474
@aayush5474 4 жыл бұрын
you can also do like vector v(n);
@niveditaprity
@niveditaprity 4 жыл бұрын
Impeccable and great explanation. Please upload more videos on tree and graph also.
@lavanyabalija699
@lavanyabalija699 4 жыл бұрын
Before listening to your lectures , am unable to write the code at 1 stretch, it used to take lot of time to debug my code. Am glad i found ur channel broo...Now am able to write code with ease. Thanxxxx for ur methodology of making coding soo easyy...Plss make more n more videos. If possible make videos on all topics of CP.
@RajveerSingh-yf1os
@RajveerSingh-yf1os 9 ай бұрын
THIS IS MY FIRST YT COMMMENT IN MY LIFE JUST LOVES THIS VIDEO
@mystryb345
@mystryb345 4 жыл бұрын
Completed heap,dp,recursion now i m.feeling confident thanks bro ...
@mohdhasnain3812
@mohdhasnain3812 3 жыл бұрын
you have studied from his channel or some where else please confirm that all the stutff you have studied in this channel is on point , effective nd easy to unxderstand
@thinkingmad1685
@thinkingmad1685 3 жыл бұрын
@@mohdhasnain3812 have you found some other channel, pls suggest btw i m feeling comfortable to understand from this channel only 🥲
@piyushaggarwal78
@piyushaggarwal78 4 жыл бұрын
Initial if and elseif condition are not required as it is redundant., Very good explanation of things, great content. Waiting eagerly for Trees and Graphs and Number theory.. I always prefer watching your videos on 2x speed , it sounds perfect that way
@SurajKumar-bw9oi
@SurajKumar-bw9oi 4 жыл бұрын
Right bro, Code written After while loop started is sufficient 👍
@pranjalsrivastava1191
@pranjalsrivastava1191 4 жыл бұрын
true, no use for the first two conditions. Already covered by the portion after while!
@Codermonk1997
@Codermonk1997 3 жыл бұрын
guys like u should be kicked on their arse, dumbo if u are so intelligent then go and try to get jon in mnc instead of watching his vidio. he is teaching how to convert ur idea into code, one if elsse will not going to increase time complexity to exponential. idiots like you exist every where.
@arunavgoel5620
@arunavgoel5620 3 жыл бұрын
@@Codermonk1997 Damm, the guy just pointed out another way to write the code, a way which reduces the number of lines and also makes the code run more efficiently...no need to get triggered by that
@___vandanagupta___
@___vandanagupta___ 3 жыл бұрын
Your every video is a blessing!
@r4ncreation686
@r4ncreation686 Жыл бұрын
best explaination of stack till date
@AnkitKumar-rh8il
@AnkitKumar-rh8il 3 жыл бұрын
maza aa gya bhaiya , aapka explain karne ka tareeka OP hai
@priyaagrawal2968
@priyaagrawal2968 2 жыл бұрын
One of the very best explanation! Thanks Aditya !
@akashverma5756
@akashverma5756 9 ай бұрын
Best Explanation of this problem online
@rishabsharma5307
@rishabsharma5307 3 жыл бұрын
Little more simpler version of code // nearest greater to right vector ngr(vector arr, int n) { stack st; vector vect; for(int i = n-1; i >= 0; i--) { while(!st.empty() && arr[i] >= st.top()) st.pop(); st.empty() ? vect.push_back(-1) : vect.push_back(st.top()); st.push(arr[i]); } reverse(vect.begin(), vect.end()); return vect; }
@rishabsharma5307
@rishabsharma5307 5 ай бұрын
Slightly better approach with no vector reversal ``` vector ngl(vector &nums) { stack st; int n = nums.size(); vector result(n, -1); for (int i = n - 1; i >= 0; i--) { while (!st.empty() && nums[i] >= st.top()) st.pop(); if (!st.empty()) result[i] = st.top(); st.push(nums[i]); } return result; } ```
@souravrajvi8193
@souravrajvi8193 2 жыл бұрын
THIS IS FUNCKIN LEGENDARY
@mohitsingh6717
@mohitsingh6717 Жыл бұрын
best channel on youtube 🙂
@rohitjha971
@rohitjha971 4 жыл бұрын
Please tree and graph bhi padhao
@TheAdityaVerma
@TheAdityaVerma 4 жыл бұрын
Will try !!
@nikhilnagrale
@nikhilnagrale 4 жыл бұрын
@@TheAdityaVerma backtracking!!!!!!!!!1
@shrutirani6828
@shrutirani6828 4 жыл бұрын
Please make more videos for placement prep... Ur videos are helping a lot.. Great mannn...
@ROHIT-gv7xk
@ROHIT-gv7xk 3 жыл бұрын
great work bro god bless you
@_solopreneur321
@_solopreneur321 3 жыл бұрын
bhaiya u rocked its great to be learning from ur videos keep adding value. Very helpful videos.
@rahulagarwal8059
@rahulagarwal8059 4 жыл бұрын
Wow.... Really happy that i found ur channel... The videos are very helpful👍
@shashijaiswal5014
@shashijaiswal5014 2 жыл бұрын
I will complete this playlist by tomorrow. 4 hr approx for this and 2hr per day. Will comment tommorow on last video of this playlist.
@anveshkhambadkar6821
@anveshkhambadkar6821 Жыл бұрын
Awesome content, Awesome Explanation Man.
@RicheshGuptaRichu
@RicheshGuptaRichu 3 жыл бұрын
Mann karta hai pdhne ka..Aesi videos dekh kar
@SreyesSrinivasan
@SreyesSrinivasan 3 жыл бұрын
Python Solution for this problem! a = [3, 0, 0, 1, 2, 4, 8] stack = list() result = [] for i in range(len(a) - 1, -1, -1): # If the stack is empty if len(stack) == 0: result.append(-1) # If the stack is not empty else: top = stack[0] # If the top element of stack is greater than a[i] if top > a[i]: result.append(top) else: # If the top element of stack is lesser than or equal to a[i] while len(stack) != 0 and stack[0]
@ritwik_shivam
@ritwik_shivam 2 жыл бұрын
Amazing bro, thanks for this code, I was looking one for python,
@sauravsingh4588
@sauravsingh4588 4 жыл бұрын
Jitne explaination deker aap pda rahe ho bhaiya utni to coding block wale wee nahi dete.
@wrushu
@wrushu 3 жыл бұрын
If we have the same question for cyclic array like.. instead of having -1 for arr[n-1] we need to look at the 0th element. What would you suggest? What I came across is to traverse the loop 2 times. for(i=2*n;i>=0;i--) and reference ith element as i%n
@satyamshukla1418
@satyamshukla1418 3 жыл бұрын
this is a gem of a content.thanks a lot for this bhaiya apne hisaab se time nikal krke agar aap backtracking graph par agar videos bana denge toh humari aur help ho jayegi.
@abhishekrai4002
@abhishekrai4002 Жыл бұрын
bhai gajab maja aagya
@Sithkar03
@Sithkar03 10 ай бұрын
Coding Ninja : Next Greater Element ( C++ Solution ) #include vector nextGreaterElement(vector& arr, int n) { // Write your code here vector ans; stack st; for(int i=n-1;i>=0;i--){ while(!st.empty() && st.top()
@keshavdutttripathi
@keshavdutttripathi 3 жыл бұрын
Great work bhai. Really helpful
@VENDETTA_9
@VENDETTA_9 9 ай бұрын
For circular array: #include using namespace std; vector nextGreaterElement(int N, vector& arr) { stack s; vector ans; for(int i=2*N-1; i>=0; i--) { if(s.empty()) ans.push_back(-1); if (!s.empty() && s.top() > arr[i%N]) ans.push_back(s.top()); else if (!s.empty() && s.top()
@NitinKumar-sx6jl
@NitinKumar-sx6jl 2 жыл бұрын
Please continue the series bhaiya great work
@manjeetkagada7534
@manjeetkagada7534 3 жыл бұрын
You are on next level man
@ayushgupta8538
@ayushgupta8538 Жыл бұрын
*Thank you so much for this awesome video.*
@NikhilTripathy
@NikhilTripathy 3 жыл бұрын
17:00 this is gem!
@sanjaydas-ck6pm
@sanjaydas-ck6pm 2 жыл бұрын
Great lecture. Small Optimization: First if and else block code are just duplicate code inside the while loop. Optimized code: ===================================== vector vec{ 1,2,0,6,5,4,1 }; vector output; stack stack; for (int i = vec.size() - 1; i >=0; i--) { while (stack.size() > 0 && stack.top()
@Tanm0609
@Tanm0609 3 жыл бұрын
very good explanation brother,keep it up and keep uploading
@NidhiSharma-sw4oz
@NidhiSharma-sw4oz 2 жыл бұрын
How O(n) complexity ????? There is a while loop inside for loop. So how it is O(n) complexity ?
@UpscWithPavan
@UpscWithPavan 7 ай бұрын
Nice, Mza Aaya bhai.
@riaria5982
@riaria5982 4 жыл бұрын
Great Explanation! To improve runtime performance don't do reverse, it's better if you insert element : for(int i=input.length-1; i>=0; i--) { while(!stack.empty() && (stack.peek()
@nishant5249
@nishant5249 4 жыл бұрын
your code is not absolutely correct
@saumitrashukla591
@saumitrashukla591 4 жыл бұрын
@@nishant5249 why? it seems correct
@AdamRubiks
@AdamRubiks 2 жыл бұрын
oh damn
@RohitKumar-uc4fw
@RohitKumar-uc4fw Жыл бұрын
Use this code to directly store from right to left. public static int[] findNextGreater(int[] ar) { int[] result = new int[ar.length]; Stack stack = new Stack(); for(int i = 0; i < ar.length; i++) { while(!stack.isEmpty() && ar[i] > ar[stack.peek()]) { int index = stack.pop(); result[index] = ar[i]; } stack.push(i); } while(!stack.isEmpty()) { int index = stack.pop(); result[index] = -1; } return result; }
@urtech5886
@urtech5886 4 жыл бұрын
Bhai bs aap videos bnana band mat kro Continue Great job
@avinashsaklani
@avinashsaklani 2 жыл бұрын
great explanation
@kapishsingh
@kapishsingh 3 жыл бұрын
its the first chanal in which i done like share and subscribe in less than 1 ms
@SanjuKumar-ye8xz
@SanjuKumar-ye8xz 3 жыл бұрын
Brother, tumne alag se vector define kiya jisase eski space complexity increase ho rhi hai.
@supritchafle3706
@supritchafle3706 4 жыл бұрын
best explanation on stack quns...
@kuldeepnarayanminj
@kuldeepnarayanminj 4 жыл бұрын
ekdum tabahi video
@Akshat-tz2de
@Akshat-tz2de Жыл бұрын
Just a small doubt if we have used only 2 loops and used break in 2nd loop when condition is met then also we could have attained same complexity. As you are also using while loop inside for loop which is running for same time that the another for loop will run and for same amount of iterations. Moreover we are using stack here hence increase in space complexity as well.
@sandip_kanzariya8476
@sandip_kanzariya8476 2 жыл бұрын
Head's of brother 👌🙏
@muhammedrameez652
@muhammedrameez652 3 жыл бұрын
Great Work bro! It really helped me
@tusharjain5658
@tusharjain5658 3 жыл бұрын
really this explanation is amazing
@annuagarwal8438
@annuagarwal8438 2 жыл бұрын
I am lucky to find your videos. Thank you for making such helpful videos. Your experience will be helpful to lot of people. Please make videos in graphs and backtracking also. I know you wont have much time. But ek mahine mai do video hi daal dena..We will wait. Thanks again😇
@375_rishavmishra9
@375_rishavmishra9 4 жыл бұрын
Bro ur great one of best teaching style.♥️♥️hope that you will continue with graph and trees also.
@shubhamdevrani7251
@shubhamdevrani7251 4 жыл бұрын
Hey bro just loved ur solution for the problem...
@ajeetworking
@ajeetworking 4 жыл бұрын
Thank you for the explaination. We can also approach this without reversing the array as vector nextLargerElement(long long arr[], int n) { stack stk; stk.push(0); vector ans(n, -1); for (int i = 1; i < n; i++) { while (!stk.empty() && arr[stk.top()] < arr[i]) { ans[stk.top()] = arr[i]; stk.pop(); } stk.push(i); } return ans; } in this, we push the element in the stack only if it is smaller than the previous element and if next is greater then we start popping until no further element is greater than the current element.
@sayantaniguha8519
@sayantaniguha8519 3 жыл бұрын
non linear time complexity
@jatinkumar4410
@jatinkumar4410 2 жыл бұрын
amazing explanation....
@imamit100
@imamit100 2 жыл бұрын
why did you stopped making videos , Aditya, please begin it again. please make these kind of worthful videos on other data structure like, array, string, linked list, queue etc. thanks
@koushik7857
@koushik7857 4 жыл бұрын
fabulous explanation!!!!
@garimadubey8970
@garimadubey8970 4 жыл бұрын
Thank you sir its very helpful vedio for us..nice explanation.
@rookieslave-kl9cq
@rookieslave-kl9cq 4 ай бұрын
dimag khul gaya bhai dimag khul gaya
@anuraagch9048
@anuraagch9048 11 ай бұрын
Python implementation a = [1, 5, 6, 6, 7, 3, 4, 12, 19, 0, 1] stack = [] res = [] # loop over every element for i in range(len(a) - 1, -1, -1): # check if stack is empty and check top element while stack and stack[-1]
@aniketsinha2826
@aniketsinha2826 2 жыл бұрын
greatly explained !!!
@bhawnabharti4369
@bhawnabharti4369 3 жыл бұрын
best explanation
@mrsmurf911
@mrsmurf911 Жыл бұрын
Thank you very much *God of DP*
@manshushivam295
@manshushivam295 2 жыл бұрын
man you won my heart
@hiteshwarmehla2267
@hiteshwarmehla2267 Жыл бұрын
Java Code : public class NextLargestElement { public static void main(String[] args) { int[] arr = {1,3,0,0,1,2,4}; int[] result = getGreaterToRight(arr); } static int[] getGreaterToRight(int[] arr){ Stack stack = new Stack(); int size = arr.length; int[] result = new int[size]; for(int i = size-1;i>=0;i--){ while (!stack.isEmpty() && stack.peek()
@Newgen123Blogspotawaytosuccess
@Newgen123Blogspotawaytosuccess 4 жыл бұрын
These are the best video series that is available till Date. Also, make your account premium so that you can continue your work in a more focussed manner. Great work. Ready to pay thousands for such beautiful content.
Hoodie gets wicked makeover! 😲
00:47
Justin Flom
Рет қаралды 138 МЛН
Миллионер | 3 - серия
36:09
Million Show
Рет қаралды 2,1 МЛН
Чистка воды совком от денег
00:32
FD Vasya
Рет қаралды 2,4 МЛН
I thought one thing and the truth is something else 😂
00:34
عائلة ابو رعد Abo Raad family
Рет қаралды 8 МЛН
Hoodie gets wicked makeover! 😲
00:47
Justin Flom
Рет қаралды 138 МЛН