2 NGR | Nearest Greater to right | Next Largest Element

  Рет қаралды 308,870

Aditya Verma

Aditya Verma

Күн бұрын

Пікірлер: 486
@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_ 3 жыл бұрын
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 6 ай бұрын
​Hi do you know from where to do queue​@@amarnathprasad1
@govindsharma6951
@govindsharma6951 9 ай бұрын
can not explain how much happy i am after finding your channel. thank u so much for creating these videos
@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
@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
@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 2 жыл бұрын
Thank you bhai
@tanya2586
@tanya2586 2 жыл бұрын
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 2 жыл бұрын
@@tanya2586 try by removing that ! Sign in the last check you have put if(!s.empty()) .
@TechnicalDrMusic
@TechnicalDrMusic Ай бұрын
Literally your DSA teaching approache is 🔥 than all others KZbins, big thank you ❤
@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!!
@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.
@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
@abhijeetgupta3461
@abhijeetgupta3461 4 жыл бұрын
reverse(v.begin(),v.end()) after for loop ...
@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
@adarshanand176
@adarshanand176 4 жыл бұрын
this is ur first impression on me... and its really looks like my dream teaching style. 🤩✌
@adityachauhan9951
@adityachauhan9951 10 ай бұрын
best explanation available on KZbin thank you so much
@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
@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 4 жыл бұрын
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
@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
@vathsan3906
@vathsan3906 4 жыл бұрын
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...
@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 Жыл бұрын
what are you doing now after 3 years of this comment ??
@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.
@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.
@theunusual4566
@theunusual4566 2 жыл бұрын
Matlab, Kamaal Ka explanation bhai. Automatically understood next 3 questions. Bahut Khood Samjhaya Aapne. Thanks.
@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 3 жыл бұрын
Thankyou
@kingrajput6202
@kingrajput6202 3 жыл бұрын
thanks man
@PankajSingh-vi3fy
@PankajSingh-vi3fy 3 жыл бұрын
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?
@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);
@malkeetsapien4852
@malkeetsapien4852 2 жыл бұрын
Brilliantly taught Sensei !
@nirbhay411
@nirbhay411 4 жыл бұрын
Bhai kya hi samjhaya he apne . maja agaya
@utsavseth6573
@utsavseth6573 Жыл бұрын
Not all heroes wear capes. Some are named aditya verma. Superb.
@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.
@niveditaprity
@niveditaprity 4 жыл бұрын
Impeccable and great explanation. Please upload more videos on tree and graph also.
@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 Жыл бұрын
@@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()
@alexamish2020
@alexamish2020 4 жыл бұрын
Ye channel bhot zyada grow krege. Gauranteed.
@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.
@animeshsingh2880
@animeshsingh2880 3 жыл бұрын
Best videos in youtube for learning DSA
@hackzyoboy2459
@hackzyoboy2459 4 жыл бұрын
165th like, 0 dislikes. That's the power of generous teaching. Great job bhaii
@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 !
@priyaagrawal2968
@priyaagrawal2968 2 жыл бұрын
One of the very best explanation! Thanks Aditya !
@aheribanerji7056
@aheribanerji7056 3 жыл бұрын
Pls, sir make more videos... it's very helpful for students like us who can't afford the high prized course .
@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 🥲
@r4ncreation686
@r4ncreation686 Жыл бұрын
best explaination of stack till date
@___vandanagupta___
@___vandanagupta___ 3 жыл бұрын
Your every video is a blessing!
@RajveerSingh-yf1os
@RajveerSingh-yf1os 11 ай бұрын
THIS IS MY FIRST YT COMMMENT IN MY LIFE JUST LOVES THIS VIDEO
@AnkitKumar-rh8il
@AnkitKumar-rh8il 4 жыл бұрын
maza aa gya bhaiya , aapka explain karne ka tareeka OP hai
@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 7 ай бұрын
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; } ```
@avinashsaklani
@avinashsaklani 3 жыл бұрын
great explanation
@abhishekanantharam645
@abhishekanantharam645 3 жыл бұрын
O bhai legend aadmi hai ye toh. Thanks✌️
@_solopreneur321
@_solopreneur321 3 жыл бұрын
bhaiya u rocked its great to be learning from ur videos keep adding value. Very helpful videos.
@vinayakrastogi890
@vinayakrastogi890 4 жыл бұрын
Bro I am waiting for your backtracking playlist... I know you have a busy schedule but please make videos on that
@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 3 жыл бұрын
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; }
@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.
@akashverma5756
@akashverma5756 11 ай бұрын
Best Explanation of this problem online
@ROHIT-gv7xk
@ROHIT-gv7xk 4 жыл бұрын
great work bro god bless you
@shrutirani6828
@shrutirani6828 4 жыл бұрын
Please make more videos for placement prep... Ur videos are helping a lot.. Great mannn...
@mohitsingh6717
@mohitsingh6717 Жыл бұрын
best channel on youtube 🙂
@souravrajvi8193
@souravrajvi8193 2 жыл бұрын
THIS IS FUNCKIN LEGENDARY
@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.
@rahulagarwal8059
@rahulagarwal8059 4 жыл бұрын
Wow.... Really happy that i found ur channel... The videos are very helpful👍
@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😇
@keshavdutttripathi
@keshavdutttripathi 3 жыл бұрын
Great work bhai. Really helpful
@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.
@shashijaiswal5014
@shashijaiswal5014 3 жыл бұрын
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.
@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 4 жыл бұрын
non linear time complexity
@375_rishavmishra9
@375_rishavmishra9 4 жыл бұрын
Bro ur great one of best teaching style.♥️♥️hope that you will continue with graph and trees also.
@NitinKumar-sx6jl
@NitinKumar-sx6jl 2 жыл бұрын
Please continue the series bhaiya great work
@anveshkhambadkar6821
@anveshkhambadkar6821 Жыл бұрын
Awesome content, Awesome Explanation Man.
@Tanm0609
@Tanm0609 3 жыл бұрын
very good explanation brother,keep it up and keep uploading
@rohitjha971
@rohitjha971 4 жыл бұрын
Please tree and graph bhi padhao
@TheAdityaVerma
@TheAdityaVerma 4 жыл бұрын
Will try !!
@nikhilnagrale
@nikhilnagrale 4 жыл бұрын
@@TheAdityaVerma backtracking!!!!!!!!!1
@manjeetkagada7534
@manjeetkagada7534 3 жыл бұрын
You are on next level man
@shubhamdevrani7251
@shubhamdevrani7251 4 жыл бұрын
Hey bro just loved ur solution for the problem...
@garimadubey8970
@garimadubey8970 4 жыл бұрын
Thank you sir its very helpful vedio for us..nice explanation.
@NidhiSharma-sw4oz
@NidhiSharma-sw4oz 2 жыл бұрын
How O(n) complexity ????? There is a while loop inside for loop. So how it is O(n) complexity ?
@sandip_kanzariya8476
@sandip_kanzariya8476 2 жыл бұрын
Head's of brother 👌🙏
@muhammedrameez652
@muhammedrameez652 3 жыл бұрын
Great Work bro! It really helped me
@jatinkumar4410
@jatinkumar4410 2 жыл бұрын
amazing explanation....
@tusharjain5658
@tusharjain5658 3 жыл бұрын
really this explanation is amazing
@vrandakansal5940
@vrandakansal5940 4 жыл бұрын
Bro!!...Great work...😄😄👌
@ayushgupta8538
@ayushgupta8538 Жыл бұрын
*Thank you so much for this awesome video.*
@abhishekrai4002
@abhishekrai4002 Жыл бұрын
bhai gajab maja aagya
@UpscWithPavanOfficial
@UpscWithPavanOfficial 9 ай бұрын
Nice, Mza Aaya bhai.
@kapishsingh
@kapishsingh 3 жыл бұрын
its the first chanal in which i done like share and subscribe in less than 1 ms
@NikhilTripathy
@NikhilTripathy 3 жыл бұрын
17:00 this is gem!
@koushik7857
@koushik7857 4 жыл бұрын
fabulous explanation!!!!
@RicheshGuptaRichu
@RicheshGuptaRichu 3 жыл бұрын
Mann karta hai pdhne ka..Aesi videos dekh kar
@vinayverma3099
@vinayverma3099 3 жыл бұрын
Isn't this also O(n^2) solution because of while loop inside a for loop.??
@adityakainthola2953
@adityakainthola2953 3 жыл бұрын
no every element is traversed atmost 2 times in both the loops so the complexity will be O(n)
@varshithkemmasaram2115
@varshithkemmasaram2115 3 жыл бұрын
@@adityakainthola2953 i did not understand bro🥲🥲
@shashankdaima8727
@shashankdaima8727 3 жыл бұрын
@@varshithkemmasaram2115 The reason this method works is because he pops every element b/w arr[i+1] to arr[n-1] which are less than arr[i] until he reached a position that is greater than arr[i] or stack becomes empty. For eg:arr:{9,7,6,5,3,2} (Strictly Decreasing sequence). Initial Stack(right should be considered as top){-1}. For last element of arr. the ans is -1 and stack becomes {-1,2}.For second last element, we will check for element greater than current element. So, currentElement=3. Stack{-1,2}. (2Pop(2); stack contains -1 so return -1. now if you move to next Element you need to push 3 into stack making{-1,3}. So, we never have to deal with 2 again. That why the complexity is O(n). as Complexity is ranging b/w n to 2n so O(n)
@aryannegi9414
@aryannegi9414 3 жыл бұрын
@@shashankdaima8727 but arent there two loops in this solution too?1st for i=n-1 to 0 and inside it while(!stack.empty&&stack.top()
@VENDETTA_9
@VENDETTA_9 11 ай бұрын
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()
@saitejdandge2315
@saitejdandge2315 4 жыл бұрын
Great lecture I got an intuition that we are using sub problem solutions to form solution current index i (Optimal substructure) and we have repetitive sub problems which is causing O(n*n). This is a dynamic programming, correct me if I'm wrong.
@supritchafle3706
@supritchafle3706 4 жыл бұрын
best explanation on stack quns...
@rajanoobrigaming1450
@rajanoobrigaming1450 4 жыл бұрын
Very helpful video vaiya ...help me lot
@SanjuKumar-ye8xz
@SanjuKumar-ye8xz 3 жыл бұрын
Brother, tumne alag se vector define kiya jisase eski space complexity increase ho rhi hai.
@Sithkar03
@Sithkar03 Жыл бұрын
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()
@redwanniloy2068
@redwanniloy2068 4 жыл бұрын
U did not show how stack's elements are storing in stack..in code...but i understand everything.. Thanks brother❤️
@prerna.please
@prerna.please 3 жыл бұрын
can you tell me , how the elements are storing?
@daddyjee6137
@daddyjee6137 3 жыл бұрын
U deserve more 🎉 ❤️❤️
@bhawnabharti4369
@bhawnabharti4369 3 жыл бұрын
best explanation
@wrushu
@wrushu 4 жыл бұрын
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
@pranjalmishra2602
@pranjalmishra2602 4 жыл бұрын
So un the worst case how is the complexity O(n) ? If has inner while loop as well... Should be N square in worst case right?
@pranjalsrivastava1191
@pranjalsrivastava1191 4 жыл бұрын
correct ... even i have a doubt in this....the inner while loop can go till n and thus worst case complexity would be n square.... the same case would be when we use 2 for loops ....please explain someone!
@pranjalmishra2602
@pranjalmishra2602 4 жыл бұрын
@@pranjalsrivastava1191 😶 👍🏻
@harshashenoy8555
@harshashenoy8555 4 жыл бұрын
Guys, while loop is over the stack And stack holds only the greatest element. Worst case happens only for one iteration where in the smaller elements gets popped. And for the next iteration the previous iteration value will be on top . So using this method the time complexity would be O(n). Try an example to understand more.
@aniketsinha2826
@aniketsinha2826 2 жыл бұрын
greatly explained !!!
@AdityaLSharma
@AdityaLSharma 2 ай бұрын
Any possibility I can get all these notes in PDF form?
@rohitmaheshwari9357
@rohitmaheshwari9357 3 жыл бұрын
bhaiya vector ko reverse krne ki jarurat nahi h ek pointer last se chla ke answer fill kr dete h tb bhi kaam chl jayega
@whatwhat3435
@whatwhat3435 4 жыл бұрын
helps a lot love frm Nepal bro
3 NGL | Nearest Greater to left
10:39
Aditya Verma
Рет қаралды 148 М.
6 Stock Span Problem
28:02
Aditya Verma
Рет қаралды 223 М.
Every team from the Bracket Buster! Who ya got? 😏
0:53
FailArmy Shorts
Рет қаралды 13 МЛН
OCCUPIED #shortssprintbrasil
0:37
Natan por Aí
Рет қаралды 131 МЛН
7 Maximum Area Histogram | MAH
35:00
Aditya Verma
Рет қаралды 216 М.
Next Greater Element | Two Variants | Leetcode
22:00
take U forward
Рет қаралды 260 М.
L5. Next Greater Element | Stack and Queue Playlist
18:25
take U forward
Рет қаралды 91 М.
11 Minimum Element in Stack in O(1) Space
26:58
Aditya Verma
Рет қаралды 115 М.
9 Rain Water Trapping
30:54
Aditya Verma
Рет қаралды 184 М.
Next Greater Element I - Leetcode 496 - Python
14:53
NeetCode
Рет қаралды 85 М.
Fast Inverse Square Root - A Quake III Algorithm
20:08
Nemean
Рет қаралды 5 МЛН
10 Minimum Subset Sum Difference
46:41
Aditya Verma
Рет қаралды 404 М.
Designing Billions of Circuits with Code
12:11
Asianometry
Рет қаралды 614 М.
Every team from the Bracket Buster! Who ya got? 😏
0:53
FailArmy Shorts
Рет қаралды 13 МЛН