Longest Consecutive Sequence | Google Interview Question | Brute Better Optimal

Sdílet
Vložit
  • čas přidán 5. 08. 2024
  • Problem Link: bit.ly/3GiWSJP
    Notes/C++/Java/Python codes: takeuforward.org/data-structu...
    We have solved the problem, and we have gone from brute force and ended with the most optimal solution. Every approach's code has been written in the video itself. Also, we have covered the algorithm with intuition.
    Full Course: bit.ly/tufA2ZYt
    You can follow me across social media, all my handles are below:
    Linkedin/Instagram/Telegram: linktr.ee/takeUforward
    0:00 Introduction
    0:52 Explanation of problem.
    1:48 Brutforce approach
    4:01 Code
    5:21 Better approach
    10:30 Code
    12:56 Optimal approach
    18:35 Code

Komentáře • 322

  • @takeUforward
    @takeUforward  Před 11 měsíci +11

    Please watch our new video on the same topic: czcams.com/video/oO5uLE7EUlM/video.html

    • @ANONYMOUS-ir8xq
      @ANONYMOUS-ir8xq Před 7 měsíci

      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 Před 23 dny

      in brute force approach time complexity is O(n^3)

    • @ANURAGDOIJODE
      @ANURAGDOIJODE Před 23 dny

      one for for loop second may be maxi subarray is n and third for linear search

  • @shubhanshusharma7457
    @shubhanshusharma7457 Před rokem +60

    this person deserves lot of respect for giving such a content for us free of cost. massive respect for you bhai.

  • @sontujain7851
    @sontujain7851 Před 7 měsíci +18

    The best part is that you just know that you solved the question just after raj draws the diagram. Amazing!!

  • @lingasodanapalli615
    @lingasodanapalli615 Před 11 měsíci +53

    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.

  • @takeUforward
    @takeUforward  Před rokem +59

    Let's march ahead, and create an unmatchable DSA course! ❤
    Use the problem links in the description.

    • @leoved1073
      @leoved1073 Před rokem +14

      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  Před rokem +18

      Yess that was the reason

    • @dayashankarlakhotia4943
      @dayashankarlakhotia4943 Před rokem +1

      Understood

    • @heybuddy_JAI_SHREE_RAM
      @heybuddy_JAI_SHREE_RAM Před rokem +2

      Thank you brother for this DSA 💥💥course

  • @sumanthvarma1908
    @sumanthvarma1908 Před rokem +64

    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

  • @ritikshandilya7075
    @ritikshandilya7075 Před 3 měsíci +5

    Literally the best content available in youtube , it really beats paid content.

  • @lucygaming9726
    @lucygaming9726 Před 5 měsíci +12

    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.

  • @leoved1073
    @leoved1073 Před rokem +32

    ** 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

  • @yourstrulysidhu
    @yourstrulysidhu Před rokem +5

    Finally found the explanation of the while loop time complexity. Thanks!

  • @cinime
    @cinime Před rokem +2

    Understood! Awesome explanation as always, thank you very very much for your effort!!

  • @prabhagaikwad4849
    @prabhagaikwad4849 Před rokem +29

    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 Před rokem +4

      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 Před rokem

      @@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 Před rokem +5

      @@yugal8627 its O(N^3) bro just do dry run

    • @krishanubiswas6869
      @krishanubiswas6869 Před 8 měsíci

      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 Před 7 měsíci

      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

  • @Manishgupta200
    @Manishgupta200 Před rokem +1

    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

  • @venkatraman7357
    @venkatraman7357 Před rokem +2

    Understood. Amazing. Keep going mate.

  • @momentos811
    @momentos811 Před rokem +1

    Your video is very helpful for everyone, thank you ❤

  • @Dhruvbala
    @Dhruvbala Před 9 měsíci

    Very well explained, brother. Thank you

  • @tonylee1868
    @tonylee1868 Před rokem +4

    Amazing work.... I will complete the whole series....

  • @iamnoob7593
    @iamnoob7593 Před 27 dny

    Thank you striver , Very well explained and Time complexity part was amazing.

  • @shubhamagarwal1434
    @shubhamagarwal1434 Před dnem

    #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.....

  • @infernogamer52
    @infernogamer52 Před rokem +7

    Whenever your heart is broken don't ever forget you're golden💯

  • @sukhpreetsingh5200
    @sukhpreetsingh5200 Před rokem +2

    Amazing explanation❤️

  • @pavansai2859
    @pavansai2859 Před 5 měsíci

    this is GOD level man!! Thank you!!

  • @sachinboreoffl
    @sachinboreoffl Před 6 měsíci +1

    Great Explanation

  • @Srinivasssssss
    @Srinivasssssss Před 7 měsíci

    Fantastic Explaination..!

  • @jagratgupta8392
    @jagratgupta8392 Před rokem

    very nice problem sir understood very nicely sir......

  • @balakrishnanr648
    @balakrishnanr648 Před rokem +1

    us, really a good one in TC Explain

  • @samuelfrank1369
    @samuelfrank1369 Před 6 měsíci

    Understood. Thanks a lot

  • @NazeerBashaShaik
    @NazeerBashaShaik Před 4 měsíci

    Understood, thank you.

  • @joelpappachan7853
    @joelpappachan7853 Před 5 měsíci

    You might be the GOAT of DSA

  • @kroax9720
    @kroax9720 Před 2 dny

    Life Changing 💕💕

  • @many987
    @many987 Před rokem +2

    when I saw this video update today, I was like what, wasn't it uploaded yesterday 😂

  • @utsavseth6573
    @utsavseth6573 Před rokem

    as usual, awesome

  • @tamilmukbang3789
    @tamilmukbang3789 Před 3 měsíci

    Understood. thank you

  • @prabhakaran5542
    @prabhakaran5542 Před 5 měsíci +1

    Understood ❤

  • @culeforever5408
    @culeforever5408 Před 9 měsíci +2

    understood 🎯

  • @infernogamer52
    @infernogamer52 Před rokem +1

    Understood Bhaiya!

  • @vaibhavkotary3949
    @vaibhavkotary3949 Před rokem +2

    sir what do you mean when you say collision happens??? can you explain with example....

  • @thebishalpaul
    @thebishalpaul Před rokem +8

    correction 13:44 hashset in java also doesn't order elements

  • @GhostVaibhav
    @GhostVaibhav Před rokem +1

    Understood🔥

  • @YourCodeVerse
    @YourCodeVerse Před 9 měsíci

    Understood✅🔥🔥

  • @rajatpatra2593
    @rajatpatra2593 Před rokem +1

    Understood ❤️

  • @itsabhinav04
    @itsabhinav04 Před rokem

    Understood, thanks :)

  • @parshchoradia9909
    @parshchoradia9909 Před rokem

    Understood Sir!

  • @user-ke7fs7ds6h
    @user-ke7fs7ds6h Před 6 měsíci +1

    awesome videos, but one correction like in brute force approach the time complexity should be o(n^3) , rest everything is perfect.

  • @HARSHA_27
    @HARSHA_27 Před 8 měsíci

    Understood!!🙇‍♂

  • @shashankgsharma0901
    @shashankgsharma0901 Před měsícem

    Understood!

  • @juhichoudhary9281
    @juhichoudhary9281 Před 9 měsíci

    Understood❤️‍🔥

  • @lakshmiprasanna7058
    @lakshmiprasanna7058 Před rokem

    Understood 💯💯💯

  • @heyOrca2711
    @heyOrca2711 Před 4 měsíci

    Understood! Sir

  • @swapnilhajare5557
    @swapnilhajare5557 Před rokem

    Understood👍🏻

  • @impalash_ag
    @impalash_ag Před měsícem

    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.

  • @sohailulislamalvi8656
    @sohailulislamalvi8656 Před 11 měsíci

    its great, thanks

  • @user-tk2vg5jt3l
    @user-tk2vg5jt3l Před 6 měsíci

    Thank you Bhaiya

  • @user-is6ky7pp2n
    @user-is6ky7pp2n Před 2 měsíci

    Understood !!

  • @shubhamdhiman5602
    @shubhamdhiman5602 Před 9 měsíci +2

    How the optimal solution is optimal, still taking O(n^2) and better solution takes only O(nlogn). Can anyone please explain this.

  • @bharathi66
    @bharathi66 Před měsícem

    Understood Bhai

  • @user-kd7ld4ff4t
    @user-kd7ld4ff4t Před 5 měsíci

    Thank you

  • @prapti2385
    @prapti2385 Před měsícem

    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?

  • @chaitanyabalanagu626
    @chaitanyabalanagu626 Před rokem +2

    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

  • @javacodegym
    @javacodegym Před rokem

    Understood 🚀🚀🚀

  • @paritoshmalhotra6309
    @paritoshmalhotra6309 Před rokem +11

    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 Před rokem +2

      And we can also use sorting to reduce it to nlogn time complexity

    • @GOJOANDSUKUNAFAN
      @GOJOANDSUKUNAFAN Před 2 měsíci

      ​@@calisthenics5247 yes correct I used hashing tho 😭

  • @dosapatiteja1588
    @dosapatiteja1588 Před 6 měsíci

    Can anyone pls explain me what is set.end() is representing here ? Im not able to get the condition in while loop .

  • @abhaymandal4903
    @abhaymandal4903 Před 11 měsíci

    Happy teacher's day striver ,
    Today is 5 th sep and i am watching this one

  • @mr_weird3680
    @mr_weird3680 Před 3 měsíci

    Thanks Brother

  • @per.seus._
    @per.seus._ Před rokem +1

    understood❤

  • @madhupadayeswanth1865

    Hello sir,
    I had a doubt that using set(ordered) just takes o(logN) know????

  • @AbhishekKumar-cv1dh
    @AbhishekKumar-cv1dh Před rokem

    Understood 🔥🎧

  • @sundarbsr4275
    @sundarbsr4275 Před rokem

    understood ♥

  • @kajiazadali7738
    @kajiazadali7738 Před rokem +6

    brute force solution t.c : O(N^3) ?

  • @sayantanpoddar5428
    @sayantanpoddar5428 Před 8 měsíci

    understood
    please came with String playlist sir

  • @shineshine9269
    @shineshine9269 Před rokem

    in better approach,wont be else condition would be else if(arr[i]-1!=lastsmaller)??

  • @ayushgupta0
    @ayushgupta0 Před rokem

    Understood.

  • @rudranshchoudhary5279
    @rudranshchoudhary5279 Před měsícem

    fabulous

  • @ajithpkumar1507
    @ajithpkumar1507 Před rokem

    understood!

  • @amansrivastava1232
    @amansrivastava1232 Před měsícem

    In my opinion, the time complexity for the brute force solution would be O(n^3)

  • @user-nk1mb5fy7j
    @user-nk1mb5fy7j Před rokem

    UNDERSTOOD

  • @SAROJKUMAR-xe8vm
    @SAROJKUMAR-xe8vm Před 6 měsíci +2

    If I can not be the part of the sequence then I will be the new sequence.

  • @konankikeerthi
    @konankikeerthi Před 2 měsíci

    Understood

  • @trojanhorse8278
    @trojanhorse8278 Před měsícem

    @15:53 worst case time complexity of find operation in unordered_set is O(N) and average case is of O(1).

  • @harshukey6072
    @harshukey6072 Před rokem +1

    Understood🎉
    40 lakh

  • @smilemka3342
    @smilemka3342 Před měsícem

    set already stores in sorted and unique mannaer right?

  • @subhrajitdas247
    @subhrajitdas247 Před rokem

    understood😍

  • @Abhay14
    @Abhay14 Před 7 měsíci

    bhaiya yha pr aapne for each loop me while loop lagaya hai yha time complexity O(n*n) nhi hoggi ?

  • @Metacognator
    @Metacognator Před rokem +2

    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

  • @vaibhavpratapsingh9956
    @vaibhavpratapsingh9956 Před rokem +9

    i think the complexity for the first brute force would be O(n^3)

  • @shaheenparween4326
    @shaheenparween4326 Před 9 měsíci

    understood✔✔

  • @WolfMan-z8k
    @WolfMan-z8k Před 3 dny

    Understand

  • @kehndihundisi6491
    @kehndihundisi6491 Před 8 měsíci

    Inspiration

  • @Leo-id4uh
    @Leo-id4uh Před 16 dny

    code 360 not show some of the code which i was done before, can anyone tell about this ?

  • @CodewithKing360
    @CodewithKing360 Před měsícem +1

    You understood brutforce and better very well but i do not understtod optimal

  • @HR-pz7ts
    @HR-pz7ts Před 3 měsíci

    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

  • @darshantawte7435
    @darshantawte7435 Před rokem

    Brute force will be O(N^3) isn't it. Assume we are given a sorted array with all consecutive integers.

  • @krisify
    @krisify Před 7 měsíci

    Why there's no trie python code in the site?

  • @sarangkumarsingh7901
    @sarangkumarsingh7901 Před 4 měsíci

    Understood.............

  • @MJBZG
    @MJBZG Před měsícem

    understood

  • @khanra17
    @khanra17 Před 6 měsíci

    I think the better one is most optimal.
    finding from set every time, doesn't make it so much slow?

  • @yogeshedekar6078
    @yogeshedekar6078 Před 8 měsíci

    Isn't the TC just O(N) for the better approach where we sort the array ? since we are not sorting and iterating in the same look the Time Complexity will be O(log(n)) for sorting and O(N) for iteration to determine the max sequence length. With O(N) dominating O(log(n)) due to beign higher thus resulting in TC of O(N) ?

    • @ajml_hnter
      @ajml_hnter Před 5 měsíci

      O(nlogn) for sorting bro, nobody can sort in O(logn), so the overall TC will be O(nlogn)

  • @I_Keshav_Prajapati
    @I_Keshav_Prajapati Před rokem +1

    the time complexity for insertion in set is O(logN) so the time complexity for this algo will be O(NlogN)

    • @himanshumewari6904
      @himanshumewari6904 Před rokem

      no its unordered set

    • @KeepCoding69
      @KeepCoding69 Před rokem +2

      For unordered_set the time complexity of every operation in set is O(1). So, after N operations the TC will be o(N) in the best and average case and O(N^2) in the worst case if collision happens.

  • @mohammadtanveer8606
    @mohammadtanveer8606 Před rokem

    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)

  • @AsharMallick750
    @AsharMallick750 Před rokem

    good video