that J < I condition is very important to keep the time complexity O(n) otherwise in case of ascending order array the time complexity will be O(n*k).
For more detailed and curated content on DSA, Web Dev etc visit nados.pepcoding.com :)
The best solution for interviews, I did meditation for the 3 points, and now I am feeling confident for the WHY of the solution.
Thankyou beta!
I am glad you liked it. I hope that you are watching till the end and trying to understand what, how, and especially why of the problem.
If you like our efforts, will you like to write a few words about us here (www.quora.com/What-are-the-good-websites-to-learn-data-structures-and-algorithms )
One of the best solutions. So intuitive!
Glad to know that you liked the content and thank you for appreciating.
Keep motivating, keep learning and keep loving Pepcoding😊
If you like our efforts, will you like to write a few words about us here (www.quora.com/How-do-I-start-learning-or-strengthen-my-knowledge-of-data-structures-and-algorithms )
amazing ,aapse accha dsa youtube pr koi bhi nahi explain kr pata ,agar aapki videos na mile to ques fas jata hai ya samajhne me bahut time lagta hai baki jaigah se ,aapse ek baar me clear ho jata hai.Thanq
The graph representation really helped me to understand the concept, I'll remember this for my life !! Definitely recommending this channel to my friends
Sumeet Sir Amazing!!!!!! Aapke alawa is question ko itne achhe se koi nahi samjha sakta! It took a lot lot effort. Pranaam aapko _/\_
Glad to know that you liked the content and thank you for appreciating.
The love and respect which I get from you people keep me highly motivated and the same I am able to forward It to you people through my videos.
So, keep motivating, keep learning and keep loving Pepcoding😊
And finally I follow only pepcoding for all coding related stuff...
Isse badiya kuch nahi 🔥😀
There are a lot of solutions on the net using a deque but this is his is highly intuitive ...Thanks for posting these solutions...but doing j=i everytime instead of only when j
Thankyou beta!
I am glad you liked it. I also hope that you are watching till the end and trying to understand the what, how, and especially why of the problem. If you like our efforts, will you like to review us here - g.page/Pepcoding/review?rc
If the numbers are in increasing order, you are essentially check each element in the window . Then time complexity will be same as brute force
If array is sorted in ascending order time complexity will be 0(n*k) so worst case complexity will be n*k correct me if I am wrong
@@gaunikasrivastava7851 @Anish suman
actually it will still be O(n). i belive your concern is related to the upgradation of j = next_grater_of_j[ j ]; since we are not starting j from I, it will only take O(1) time every time. if we start j from I each time then of course time complexity will be O(n*k). for elements 1 2 3 4 5 6 7 8 9. if window size is 4. then first time while loop will run 3 times and next greater element will be 4. next time same while loop will start from 4 and will run only once i.e. till 5. and then only once till 6 and so on. so we get O(N) solution . however if we start j =i each time then time complexity increases to O(N*K).
Youe explanantion is undoubtedly the best !
The time complexity would still be O(N*K).
Take the case when array is sorted in increasing order.
Best Solution with best explanation
very intuitive best solution much better than deque one highly appreciated
Far better then those boring " PHD'S " Professors explanation....
Thankyou sir! Amazing video.
sir, please post one for max frequency stack too. Thankyou!
one of the amazing explanation of this question.
Thanks Ritika :) If you want more content like this please visit nados.pepcoding.com
best solution ever. Thank you sir!!!!
TC for this code is O(n)??
Thankyou beta,
I am glad you liked it. I also hope that you are watching till end.
If you like our efforts, will you like to write a few words about us here (www.quora.com/How-do-I-start-learning-or-strengthen-my-knowledge-of-data-structures-and-algorithms )
I don't think so because within first for loop he is using while loop to increment j
If i == j is taken everytime in the loop then the TC = n*k in the worst case when the array is sorted
If the other one is taken then it is linear
whats the time complexity of this approach?
on leetcode , this approach is showing TLE
God level explaination!
Thank u sp much sir for ur efforts and motivational reels.
sir 1 small doubt here say i have an array 1 8 12 16 and then the next greater array would be 8 12 16 4 but when you wrote the statement nge[j]
ok got it actually it is the index of elements that is being stored in next greater element array thanks sumit sir!
Sir aapne nge[j] < i+k kra h iski jgh
nge[j]< nge[i+k] ni aana chaiye kyuki agar hmara j =1 h or nge[j] = 9 hua to
9 < 1+4 is not true?
So intuitive! Hats-off & God-bless you!
Many many thanks. For better experience visit on nados.pepcoding.com, Also follow our Instagram account to stay tuned.
Many many thanks. For better experience please explore nados.pepcoding.com
Also don't forget to follow our Instagram account.
thanz sir samajh me aa gaya
Great explanation sir!!!
Epic level teaching
Great content!
Sir this can be done using deque too in O(n)
Agreed. Also, that is easier. If you watch closely this is doing the same thing as Dequeue.
Sir pleaseeeeee upload the level up course further please
Thank you sir
at line no. 21 , it should be st.push(arr[ arr.length-1]);
For better insight, visit nados.io, post your doubts, community will help you out there.
Very Nice Explanation Sir......Keep making videos
I am glad you liked it. I also hope that you are watching till end and trying to understand the what, how and especially why of the problem.
If you like our efforts, we request a review
g.page/Pepcoding/review?rc
You can subscribe to our channel here
th-cam.com/users/Pepcodingabout?view_as=subscriber
Sir, how do we meditate over a question? If you can give a way for that then that would be great.
this vid is from which
playlist?
behtareen
"If the numbers are in increasing order, you are essentially check each element in the window . Then time complexity will be same as brute force"...
What to do in this case ,sir??
please reply!!
sir where is the link of that prerequisite video ?
Sir,what if j becomes greater than i,
as you only write smaller than condition ..i.e-if(j
Sir, can we solve this question using reverse or maximum priority queue?
Sir you are great
Done!
if the array is in increasing order then j will jump to each window element which lead to n^2 time
really good sir
Keep watching. sign up on nados.io for better experience and well organised content.
Hi sir , it is helpful if you provide the vedio link of the questions which is prerequsite for the current questions. or refference vedios
Sometimes I feel very amazed, how a channel with such a awesome content have only reached 53k subs in last 3years,
the team and specially Sumeet Sir deserves a minimum of 100k subs...hope they reach this milestone soon.
Just Wow❣
Glad to know that you love our video, for better experience.
Visit - nados.pepcoding.com and sign up to NADOS.
Don't forget to follow us on Instagram instagram.com/pepcoding/
Goddamn this guy!!!!
best for a reason
PLEASE UPLOAD EASY TO ANALYZE INPUT
Superb!
Thanks a lot!
For better experience and well-organised content
visit- nados.pepcoding.com
Also you can post your query on community tab.
Don't forget to follow us on Instagram
instagram.com/pepcoding/
Sir, jo video dekhne bole hai as a prerequisite, uska link bhi dijiye, 2:45
Wouldnt it be O(n^2) for cases like 1,2,3..n and k=n/2
For such queries sign up on nados.io
You can post such queries on community tab, our alumni and mentors will guide you.
awesome!
sir, the solution which you have discussed is taking O(nk) time. ex- 1,2,3,4,5,6,7,8,9,10. for this j will jump 3 times for every window. i.e. , when array is sorted , time complexity will be o(nk). right??
Nope, it'll jump 3 times for 1st window and only 1 time for all subsequent windows. Print i and j in the inner loop for your example.
@@Pepcoding The 'if' condition instead of while to update 'j' prevents O(n*k), right ?
@@codemaestro9904 no, it is the fact that we jump don't just increase j by 1, instead we take it to nge
Sir j peeche chut gya toh age lake barabar krdere, jb j age nikl jara tb ka?
Beta, I regret to inform you that, I won't be able to answer/solve the personal doubts of each and every student over here. For clearing your doubts, you can join our community on telegram - t.me/pepcoding.
Also, we have a premium facility available for the students in which you can get the 12 hours doubt support facility. Jisme aap agr kisi bhi question main kahin bhi faste ho to aap doubt support par reach kar skte ho aur aapko TA assign ho jayega and you can get your doubt resolved from them.
You look like rakshit shetty brother 😁
by the way it was really good explanation 👌🏿
🤭 wow, this cheers me up. I am glad we at pepcoding could be of help to you. Keep learning. Also, recommend us to your juniors and peers, they may also benefit.
Sir what is the time complexity of this solution and how can we optimize it??
Sir, ismai agar next greater element window ke bahar hoota h toh uss i ke liye whi greatest element hai...woh j kaise detect kar raha idhar...konse part of code m aa raha woh nhi smj aaya...
thats why we're always doing j = i, because if for a window the NGE is outside then J will be at the correct position.
@@revaanmishra but we are doing that only when ji ?
how to bring back that to i?
@@siddhantprakash. har loop chalne ke baad vo sliding window ke andar ya i se peche hoga
agar peeche hai to humne same to i laane ka code krdiya
aage hai to koi dikkat ki baat nahi hai cuz check bhi hume aage krna hai as sliding window goes ahead
I was able to solve 99% question myself but missed that j < i condition due to which I got TLE on LC. Thanks for this explanation.
amazing : )
woow
Sir aap badass ho bhaai!. Kya mast he kar dete ho.Please open a patreon, I will pay. Thanks
Haha..bro aage 5 saal baad jab apna band bnayunga aur nya youtube channel open kruga tab open kruga patreon😋
@@Pepcoding 🤣🤣🤣🤣 sir aap logic mast clear Karne k Saath comedy b Kar dee hi, nice!
why these que time complexity is not n square
Sir howz the while is working in last FOR loop without incrementing j ? Line no 47
Beta, I regret to inform you that, I won't be able to answer/solve the personal doubts of each and every student over here. For clearing your doubts, you can join our community on telegram - t.me/pepcoding.
you need to check that again we are evidently jumping on next nge and placing our j on that pos. {j = nge[j]}
write down the code in paper and trace every iteration.. you will get it..
what about [1.-1] k=1;?
Line 36 you pushed i but not arr[I]
One solution might be with using double ended queque🤔
Wow................ : ) : )
It is giving tle on ib😞
ek baar aapka solution post kijie. koshish karte hain. complexity iski O(n) he hai.
@@Pepcoding
Sir this code is giving tle on gfg ide. Please help sir.
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG {
public static void main (String[] args) {
//code
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
for(int r=0;r
J=I shi h
for this solution it wouldnt work if input is
10
1 2 3 4 1 2 3 4 5 6
4
it gives tle
awesome !
@@Pepcoding sir I have one doubt , time complexity of this is always O(N) right ?
One of the most underrated channels in coding. You're fabulous Sir! :)
Thank you for appreciating.
The love and respect which I get from you people keep me highly motivated and the same I am able to forward It to you people through my videos.
So, keep motivating, keep learning and keep loving Pepcoding😊
@@Pepcoding what is the meaning of pepcoding /