Longest Consecutive Sequence | Google Interview Question | Brute Better Optimal

  Рет қаралды 234,164

take U forward

take U forward

Күн бұрын

Пікірлер: 347
@takeUforward
@takeUforward Жыл бұрын
Please watch our new video on the same topic: kzbin.info/www/bejne/pYCYpn97bKqIoq8
@ANONYMOUS-ir8xq
@ANONYMOUS-ir8xq 9 ай бұрын
striver i am totally confused with the time complexity in optimal approach to find an element in set we need only o(1)??
@ANURAGDOIJODE
@ANURAGDOIJODE Ай бұрын
in brute force approach time complexity is O(n^3)
@ANURAGDOIJODE
@ANURAGDOIJODE Ай бұрын
one for for loop second may be maxi subarray is n and third for linear search
@jeevankurian9148
@jeevankurian9148 Ай бұрын
@9:45 It was like Raj is showing middle finger to us😂.
@shubhanshusharma7457
@shubhanshusharma7457 Жыл бұрын
this person deserves lot of respect for giving such a content for us free of cost. massive respect for you bhai.
@lingasodanapalli615
@lingasodanapalli615 Жыл бұрын
I think the brute force is not equal to N*N. But it is N*N*N. Because for every outer loop the inner loop will be traversed N*N times.
@b_01_aditidonode43
@b_01_aditidonode43 10 ай бұрын
yes i think the same, it should be N^3
@sandipanchakraborty8489
@sandipanchakraborty8489 9 ай бұрын
Correct O(N * N * N)
@alexmercer416
@alexmercer416 6 ай бұрын
how? can u explain pls @@sandipanchakraborty8489
@alexmercer416
@alexmercer416 6 ай бұрын
how can u explain pls ? @@b_01_aditidonode43
@Rahul_Mongia
@Rahul_Mongia 4 ай бұрын
no
@sontujain7851
@sontujain7851 9 ай бұрын
The best part is that you just know that you solved the question just after raj draws the diagram. Amazing!!
@sumanthvarma1908
@sumanthvarma1908 Жыл бұрын
I believe that you #striver will remain forever in the hearts of lakhs of students around the world ❤ You are more than virat kohli for me because no one can come closer to your selflessness regarding contribution to the community Two people are my inspiration in this world #Virat #Striver For your HardWork and dedication
@yowaimo890
@yowaimo890 8 ай бұрын
bhai wo zinda h
@khanra17
@khanra17 7 ай бұрын
🤣🤣@@yowaimo890
@lucygaming9726
@lucygaming9726 6 ай бұрын
For anyone following the SDE Sheet, solve all problems of Disjoint Set Union (DSU) in Graph Series first. This question can be easily solved using DSU and very easy to come up too. Again, thanks to Striver for creating an awesome Graph playlist.
@futuretl1250
@futuretl1250 Ай бұрын
how?can you give some hint?
@RaunitJaiswal-s9v
@RaunitJaiswal-s9v 4 күн бұрын
for (int i = 0; i < n; i++) { if (a[i] - 1 == lastSmaller) { cnt += 1; } cnt = 1; // This line now always resets `cnt` to 1 in every iteration lastSmaller = a[i]; longest = max(longest, cnt); } for those who are thinking what if we remove the else if statement but keep the code inside it in the loop, the code would look like above
@prabhagaikwad4849
@prabhagaikwad4849 Жыл бұрын
I implemented the brute force first, the time complexity is O(n^3) as we need to start over the search for every found consecutive number. As array may be in sorted order.
@tusharvlogs6333
@tusharvlogs6333 Жыл бұрын
glad i found this. was also thinking the same how can it be n^2 . because like if we have 104,101,102,103. so for 101 i need to check the whole array again then when i reach 103 again from starting .
@yugal8627
@yugal8627 Жыл бұрын
@@tusharvlogs6333 For every element you are traversing the whole array , to traverse each element it is O(N) and for each element you are traversing the array again so another O(N) , i.e., O(N^2)
@ShivamSingh-gk3qu
@ShivamSingh-gk3qu Жыл бұрын
@@yugal8627 its O(N^3) bro just do dry run
@krishanubiswas6869
@krishanubiswas6869 9 ай бұрын
thanks for the confirmaton...i was also thinking that...striver may have forgotten to take into account the time complexity of the linear search part....
@omrasal7945
@omrasal7945 8 ай бұрын
int longestSuccessiveElements(vector& a) { sort(a.begin(), a.end()); int n = a.size(); int maxCount = 1, c = 1; for (int i = 1; i < n; i++) { if (a[i] == a[i - 1] + 1) { c++; maxCount = max(maxCount, c); } else if (a[i] != a[i - 1]) { c = 1; } } return maxCount; } more simple app
@ritikshandilya7075
@ritikshandilya7075 4 ай бұрын
Literally the best content available in youtube , it really beats paid content.
@CodewithKing360
@CodewithKing360 2 ай бұрын
You understood brutforce and better very well but i do not understtod optimal
@infernogamer52
@infernogamer52 Жыл бұрын
Whenever your heart is broken don't ever forget you're golden💯
@yourstrulysidhu
@yourstrulysidhu Жыл бұрын
Finally found the explanation of the while loop time complexity. Thanks!
@RaunitJaiswal-s9v
@RaunitJaiswal-s9v 4 күн бұрын
Initial State: lastSmaller starts with INT_MIN, an extremely small value that will be replaced with the first element of the array once we begin processing. First Condition (if (a[i] - 1 == lastSmaller)): This condition checks if the current element a[i] is exactly one greater than lastSmaller, meaning it is the next element in a consecutive sequence. If this is true, the current element a[i] is part of the current consecutive sequence, so cnt is incremented to count this element, and lastSmaller is updated to a[i]. Second Condition (else if (a[i] != lastSmaller)): If a[i] is not consecutive to lastSmaller (i.e., a[i] is not equal to lastSmaller + 1), the code checks if it is different from lastSmaller. This condition avoids counting duplicates in the sorted array. If the element is different, it starts a new sequence with cnt reset to 1, and lastSmaller is updated to the current element a[i].
@thebishalpaul
@thebishalpaul Жыл бұрын
correction 13:44 hashset in java also doesn't order elements
@manavpatnaik222
@manavpatnaik222 8 ай бұрын
Correct
@time7192
@time7192 14 күн бұрын
+1
@takeUforward
@takeUforward Жыл бұрын
Let's march ahead, and create an unmatchable DSA course! ❤ Use the problem links in the description.
@leoved1073
@leoved1073 Жыл бұрын
Idk why did you upload this video again but if you did just because better solution had edge case of multiple duplicate elements then hats off to you man ❤ even though it was negligible mistake still you re-recorded that part and uploaded video again I really appreciate your efforts 🫂
@takeUforward
@takeUforward Жыл бұрын
Yess that was the reason
@dayashankarlakhotia4943
@dayashankarlakhotia4943 Жыл бұрын
Understood
@heybuddy_JAI_SHREE_RAM
@heybuddy_JAI_SHREE_RAM Жыл бұрын
Thank you brother for this DSA 💥💥course
@paritoshmalhotra6309
@paritoshmalhotra6309 Жыл бұрын
We can use unordered_map to replace the Linear Search in the brute force solution to bring down it's complexity to O(n^2).
@calisthenics5247
@calisthenics5247 Жыл бұрын
And we can also use sorting to reduce it to nlogn time complexity
@GOJOANDSUKUNAFAN
@GOJOANDSUKUNAFAN 3 ай бұрын
​@@calisthenics5247 yes correct I used hashing tho 😭
@vaibhavpratapsingh9956
@vaibhavpratapsingh9956 Жыл бұрын
i think the complexity for the first brute force would be O(n^3)
@takeUforward
@takeUforward Жыл бұрын
Yes you are correct
@vivekpatwal4655
@vivekpatwal4655 Жыл бұрын
agree
@leoved1073
@leoved1073 Жыл бұрын
** Timestamps ** 0:00 Introduction 0:52 Explaination of problem. 1:48 Brutforce approach 4:01 Code 5:21 Better approach 10:30 Code 12:56 Optimal approach 18:35 Code
@many987
@many987 Жыл бұрын
when I saw this video update today, I was like what, wasn't it uploaded yesterday 😂
@ksankethkumar7223
@ksankethkumar7223 Жыл бұрын
I guess the brute force technique will take O(N^3) and not O(N^2) because for every element we have to linear search until we break out of the longest sequence
@nagame859
@nagame859 Жыл бұрын
Nice observation!
@RajeevCanDev
@RajeevCanDev Жыл бұрын
That comes under a manually defined function which will not be counted in the loop and would have an another O(n) TC ...as per my observation 🧐
@ksankethkumar7223
@ksankethkumar7223 Жыл бұрын
@@RajeevCanDev It will run in the loop even though it is user defined function and hence TC boils down to N^3
@RajeevCanDev
@RajeevCanDev Жыл бұрын
@@ksankethkumar7223 yeahh true👍🏻, but may be there is an another logic by which striver stated it as TC O(N²) or maybe he is wrong
@ksankethkumar7223
@ksankethkumar7223 Жыл бұрын
@@RajeevCanDev idk about that but the approach he mentioned is O(N^3)
@lillyput2275
@lillyput2275 16 күн бұрын
thanku so much striver ji ...loads of love
@tonylee1868
@tonylee1868 Жыл бұрын
Amazing work.... I will complete the whole series....
@user-ke7fs7ds6h
@user-ke7fs7ds6h 8 ай бұрын
awesome videos, but one correction like in brute force approach the time complexity should be o(n^3) , rest everything is perfect.
@cinime
@cinime Жыл бұрын
Understood! Awesome explanation as always, thank you very very much for your effort!!
@impalash_ag
@impalash_ag 2 ай бұрын
Hi Raj, the TC for the BF solution should be O(n^3) and not O(n^2) since there are 3 nested loops and inner while loop will run for nearly n times and same goes for the function that does the linear search.
@Manishgupta200
@Manishgupta200 Жыл бұрын
Time complexity explaination in depth by yours after every explaination of problem is really really helpful that i love the most. And better and optimat solution here is really cool. I solved better solution by myself before watching your tutorial. Thankyou Striver
@iamnoob7593
@iamnoob7593 2 ай бұрын
Thank you striver , Very well explained and Time complexity part was amazing.
@amansrivastava1232
@amansrivastava1232 3 ай бұрын
In my opinion, the time complexity for the brute force solution would be O(n^3)
@kajiazadali7738
@kajiazadali7738 Жыл бұрын
brute force solution t.c : O(N^3) ?
@tarunrajput6865
@tarunrajput6865 3 ай бұрын
No bro
@kajiazadali7738
@kajiazadali7738 3 ай бұрын
Why not ​@@tarunrajput6865
@venkatraman7357
@venkatraman7357 Жыл бұрын
Understood. Amazing. Keep going mate.
@chaitanyabalanagu626
@chaitanyabalanagu626 Жыл бұрын
Using treeset would also be a better solution, as adding elements into treeset takes nlogn time and then parsing and checking is easy after that
@Metacognator
@Metacognator Жыл бұрын
I think we need to consider the time complexity of underlying data structure as well I mean if we are using set.find() then it not happens in O(1) time and we have not considered its time complexity... we can insted go for unordered map
@takeUforward
@takeUforward Жыл бұрын
unordered set works for O(1)
@arpitkumarmishra2734
@arpitkumarmishra2734 Жыл бұрын
@@takeUforward but on gfg it says O(N)..?
@kroax9720
@kroax9720 Ай бұрын
Life Changing 💕💕
@trojanhorse8278
@trojanhorse8278 2 ай бұрын
@15:53 worst case time complexity of find operation in unordered_set is O(N) and average case is of O(1).
@culeforever5408
@culeforever5408 10 ай бұрын
understood 🎯
@vaibhavkotary3949
@vaibhavkotary3949 Жыл бұрын
sir what do you mean when you say collision happens??? can you explain with example....
@surbhirathore._
@surbhirathore._ 3 ай бұрын
What's wrong in this code! It's working for half test cases not all Int n = a.length; Int largest = 1; for(int I=0 ; I
@raorao7329
@raorao7329 2 ай бұрын
bro it should be i
@meghasharma4875
@meghasharma4875 Ай бұрын
i directly got better approach in mind
@mdfaizanmdfaizan6041
@mdfaizanmdfaizan6041 12 күн бұрын
But we could solve this question with time complexity o(n square logn) first we sort the array and then travel through the each element of the array
@RaunitJaiswal-s9v
@RaunitJaiswal-s9v 4 күн бұрын
lastsmaller at the very first would confuse but just think like these on the very first step it will get over right by the very first element of your array cause its not equal just dont get confuse guys
@prabhakaran5542
@prabhakaran5542 6 ай бұрын
Understood ❤
@parica117
@parica117 17 күн бұрын
understood striver , thank you
@momentos811
@momentos811 Жыл бұрын
Your video is very helpful for everyone, thank you ❤
@joelpappachan7853
@joelpappachan7853 6 ай бұрын
You might be the GOAT of DSA
@sachinboreoffl
@sachinboreoffl 7 ай бұрын
Great Explanation
@harshukey6072
@harshukey6072 Жыл бұрын
Understood🎉 40 lakh
@shubhamagarwal1434
@shubhamagarwal1434 Ай бұрын
#Free Education For All.. # Bhishma Pitamah of DSA...You could have earned in lacs by putting it as paid couses on udamey or any other elaerning portals, but you decided to make it free...it requires a greate sacrifice and a feeling of giving back to community, there might be very few peope in world who does this...."विद्या का दान ही सर्वोत्तम दान होता है" Hats Off to you man, Salute from 10+ yrs exp guy from BLR, India.....
@aryanchauhan2571
@aryanchauhan2571 26 күн бұрын
Say that again, after you discover something called “TUF+”
@kabilm8040
@kabilm8040 15 күн бұрын
I TRIED MYSELF T.C = O(NlogN) and S.C = O(1) class Solution { public: int longestConsecutive(vector& nums) { int n = nums.size(); sort(nums.begin(), nums.end()); int count =1; int maxCount = INT_MIN; if(nums.begin()==nums.end()) return 0; for(int i=0;i
@nikhild.20
@nikhild.20 10 ай бұрын
Instead of iterating in set, we can iterate in array as well, right? int longestSuccessiveElements(vector&a) { // Write your code here. setst; for(int i=0;i
@nirbhaybhardwaj4330
@nirbhaybhardwaj4330 Ай бұрын
can you explain me bro why we using " == st.end() " and " != st.end()" ...... he used in many problems but i don't understood......
@pavansai2859
@pavansai2859 6 ай бұрын
this is GOD level man!! Thank you!!
@shubhamdhiman5602
@shubhamdhiman5602 10 ай бұрын
How the optimal solution is optimal, still taking O(n^2) and better solution takes only O(nlogn). Can anyone please explain this.
@SAROJKUMAR-xe8vm
@SAROJKUMAR-xe8vm 7 ай бұрын
If I can not be the part of the sequence then I will be the new sequence.
@balakrishnanr648
@balakrishnanr648 Жыл бұрын
us, really a good one in TC Explain
@Dhruvbala
@Dhruvbala 11 ай бұрын
Very well explained, brother. Thank you
@sukhpreetsingh5200
@sukhpreetsingh5200 Жыл бұрын
Amazing explanation❤️
@mohammadtanveer8606
@mohammadtanveer8606 Жыл бұрын
python optimal solution: longest=1 hashmap={} count=0 n=len(arr) for i in range(n): num=arr[i] hashmap[num]=1 for j in range(n): num=arr[j] count=1 while num+1 in hashmap: count+=1 num+=1 longest=max(longest,count) return longest i used hashmap to access the elements which will be taking o(1) complexity so o(n+n) will be time complexity and sc-->o(n)
@shreekarpasupuletia2269
@shreekarpasupuletia2269 27 күн бұрын
please write the ode for 1st and 2nd method also in coming videos
@infernogamer52
@infernogamer52 Жыл бұрын
Understood Bhaiya!
@JK-de2gh
@JK-de2gh Ай бұрын
understood
@HR-pz7ts
@HR-pz7ts 5 ай бұрын
what if all size of list is n and all elements are distant to each other by a difference of 2. Meaning the longest consecutive subseq is 1. Then this algo is taking TC of n^2
@shashankgsharma0901
@shashankgsharma0901 2 ай бұрын
Understood!
@RohitRaj-kr7ti
@RohitRaj-kr7ti Жыл бұрын
Hey, The complexity in brute force is N^3, as there is a linear search too. In the optimal code, one point that can be corrected is that while you say that we will iterate over set but essentially you end up reiterating the input array again.. I would request to use the set numbers only, convert them to array and then use it only. int[] numsFromSet = hashSet.stream().mapToInt(Integer::intValue).toArray(); @takeUforward
@gourabbhattacharjee2128
@gourabbhattacharjee2128 11 ай бұрын
Why are you converting the hashSet to an array? This operation takes another O(set size) for unboxing the elements. And you have no other option other than linear search to search for consecutive elements using the array approach. Use the set.contains() while iterating through the set itself.
@user-mt4jk5gq7g
@user-mt4jk5gq7g 16 күн бұрын
i also thinks that the time complexity of brute could be best expressed as O(n raised to 3) in the worst case.Please respond.
@diggl3358
@diggl3358 Жыл бұрын
Why we don't use an array of frecvente, also can use and bitsed for memories, complexity will be O(n*max) max mean the maximum number, and we read n number and after with for we go through maximum number
@diggl3358
@diggl3358 Жыл бұрын
Bitset b; Int maxi; Int main(){ Int n; For(int i=1;i>x; b[x] = 1; If( x > maxi ) maxi=x; } Int length=0,cnt=0; For(int i=1;i length) length = cnt; If( b[i] == 0) cnt = 0; } Cout
@sayantanpoddar5428
@sayantanpoddar5428 10 ай бұрын
understood please came with String playlist sir
@cameye9
@cameye9 3 ай бұрын
We can solve this question in O(N) Time Complexity and O(1) Space Complexity, by taking previous int arr[0] and comparing from index 1 to it's difference is either 0 or 1 if it is 0 continue and else if 1 increments count++ and update maxLen=Math.max(maxLen,count) and else count =1 and previous will be arr[i] current value. By that approach we can easily solve in O(N) time and O(1) space.🙂
@GOJOANDSUKUNAFAN
@GOJOANDSUKUNAFAN 3 ай бұрын
Kinda true only for this array
@yugal8627
@yugal8627 Жыл бұрын
I think this solution will never come in mind of someone who didn't seen this type of problem.
@GhostVaibhav
@GhostVaibhav Жыл бұрын
Understood🔥
@ShivamSingh-gk3qu
@ShivamSingh-gk3qu Жыл бұрын
107,106,105,104,103,102,101,100 for this case the time complexity even using Hashset will be O(N^2+N), bcoz if we are using unordered set then we have iterate from 107 to 100 and when we reach 100 then again we'll iterate using while loop to get the longest sequence. Someone plz correct me if i am wrong ........
@pradhyumnapalore5942
@pradhyumnapalore5942 Жыл бұрын
No, it is not the case as we are firstly iterating from 107 to 101 and as these are not the first element, while loop will never run while the loop will run for 100 only So, we are only iterating the whole array again for 100, not for all the cases
@ali_dev7415
@ali_dev7415 Жыл бұрын
you are missing if condition, if 107 is not the starting element then while won't run
@gautamsaxena4647
@gautamsaxena4647 8 күн бұрын
understood bhaiya
@ParasSharma-mf8or
@ParasSharma-mf8or Жыл бұрын
This same video was uploaded yesterday also
@divyangdheer7292
@divyangdheer7292 Жыл бұрын
Nd then deleted
@Srinivasssssss
@Srinivasssssss 8 ай бұрын
Fantastic Explaination..!
@oyeesharme
@oyeesharme 3 сағат бұрын
thanks bhaiya
@NazeerBashaShaik
@NazeerBashaShaik 5 ай бұрын
Understood, thank you.
@arnavkumar4631
@arnavkumar4631 6 күн бұрын
Understood
@samuelfrank1369
@samuelfrank1369 7 ай бұрын
Understood. Thanks a lot
@rajatpatra2593
@rajatpatra2593 Жыл бұрын
Understood ❤️
@jagratgupta8392
@jagratgupta8392 Жыл бұрын
very nice problem sir understood very nicely sir......
@abhaymandal4903
@abhaymandal4903 Жыл бұрын
Happy teacher's day striver , Today is 5 th sep and i am watching this one
@sarthakkharade7112
@sarthakkharade7112 22 күн бұрын
understood sir
@tamilmukbang3789
@tamilmukbang3789 5 ай бұрын
Understood. thank you
@YourCodeVerse
@YourCodeVerse 10 ай бұрын
Understood✅🔥🔥
@heyOrca2711
@heyOrca2711 5 ай бұрын
Understood! Sir
@HARSHA_27
@HARSHA_27 9 ай бұрын
Understood!!🙇‍♂
@sumitkawade9396
@sumitkawade9396 Жыл бұрын
Can anyone explain if(st.find(it-1)==st.end()) && if(st.find(x+1)!=st.end())
@himanshukaushik9223
@himanshukaushik9223 Жыл бұрын
First element ka previous search kar raye hai agar vo == end ho Gaya matlab vo nahi ha set ma this means it is 1st ele otherwise vo 1st ele nahi hoga
@AbjSir
@AbjSir 11 ай бұрын
I think brute should be O(n!) Correct me please if i'm wrong
@khanra17
@khanra17 7 ай бұрын
I think the better one is most optimal. finding from set every time, doesn't make it so much slow?
@dhruvnivatia9222
@dhruvnivatia9222 Ай бұрын
This is O(N^2) solution, better to sort the array. '
@prapti2385
@prapti2385 2 ай бұрын
In the optimal approach, is there any way. we could remove the elements from the set after checking. For example when we check for 100, theres no element before it as it is the starting element. Can we remove 101 and 102 after counting it as the longest consecutive subsequence?
@WolfMan-z8k
@WolfMan-z8k Ай бұрын
Understand
@juhichoudhary9281
@juhichoudhary9281 10 ай бұрын
Understood❤️‍🔥
@tusharsrivastava107
@tusharsrivastava107 14 күн бұрын
Can we not make it easy by using Sorted Set in java ?
@MaheshPatil-of1zy
@MaheshPatil-of1zy 4 ай бұрын
I think the brute force complexity is o(N^3).
@bharathi66
@bharathi66 2 ай бұрын
Understood Bhai
@smilemka3342
@smilemka3342 2 ай бұрын
set already stores in sorted and unique mannaer right?
@lakshmiprasanna7058
@lakshmiprasanna7058 Жыл бұрын
Understood 💯💯💯
@per.seus._
@per.seus._ Жыл бұрын
understood❤
@utsavseth6573
@utsavseth6573 Жыл бұрын
as usual, awesome
@user-is6ky7pp2n
@user-is6ky7pp2n 3 ай бұрын
Understood !!
@parshchoradia9909
@parshchoradia9909 Жыл бұрын
Understood Sir!
Set Matrix Zeroes | O(1) Space Approach | Brute - Better - Optimal
30:07
Next Permutation - Intuition in Detail 🔥 | Brute to Optimal
28:15
take U forward
Рет қаралды 385 М.
Bend The Impossible Bar Win $1,000
00:57
Stokes Twins
Рет қаралды 43 МЛН
拉了好大一坨#斗罗大陆#唐三小舞#小丑
00:11
超凡蜘蛛
Рет қаралды 16 МЛН
Blue Food VS Red Food Emoji Mukbang
00:33
MOOMOO STUDIO [무무 스튜디오]
Рет қаралды 34 МЛН
Секрет фокусника! #shorts
00:15
Роман Magic
Рет қаралды 72 МЛН
Medium Google Coding Interview With Ben Awad
51:27
Clément Mihailescu
Рет қаралды 1,3 МЛН
Leaders in an Array | Brute - Optimal | Strivers A2Z DSA Course
11:53
take U forward
Рет қаралды 127 М.
Mastering Dynamic Programming - How to solve any interview problem (Part 1)
19:41
Leetcode 128 - LONGEST CONSECUTIVE SEQUENCE
9:24
NeetCode
Рет қаралды 349 М.
Kadane's Algorithm | Maximum Subarray Sum | Finding and Printing
20:09
take U forward
Рет қаралды 412 М.
Spiral Traversal of a Matrix | Spiral Matrix
16:33
take U forward
Рет қаралды 204 М.
The Last Algorithms Course You'll Need by ThePrimeagen | Preview
16:44
Frontend Masters
Рет қаралды 318 М.
How to Solve ANY LeetCode Problem (Step-by-Step)
12:37
Codebagel
Рет қаралды 204 М.
Count Subarray sum Equals K | Brute - Better -Optimal
24:09
take U forward
Рет қаралды 285 М.
Bend The Impossible Bar Win $1,000
00:57
Stokes Twins
Рет қаралды 43 МЛН