Longest Consecutive Sequence | Google Interview Question | Brute Better Optimal

แชร์
ฝัง
  • เผยแพร่เมื่อ 27 ธ.ค. 2024

ความคิดเห็น • 393

  • @takeUforward
    @takeUforward  ปีที่แล้ว +18

    Please watch our new video on the same topic: th-cam.com/video/oO5uLE7EUlM/w-d-xo.html

    • @ANONYMOUS-ir8xq
      @ANONYMOUS-ir8xq ปีที่แล้ว

      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 5 หลายเดือนก่อน

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

    • @ANURAGDOIJODE
      @ANURAGDOIJODE 5 หลายเดือนก่อน

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

    • @jeevankurian9148
      @jeevankurian9148 4 หลายเดือนก่อน

      @9:45 It was like Raj is showing middle finger to us😂.

    • @AshishPrajapati-j7r
      @AshishPrajapati-j7r 2 วันที่ผ่านมา

      this is what I wrote before looking at better approach
      const findLongestConsecutiveLength = (arr) => {
      let longest_consecutive = 1;
      let max_longest_consecutive = 1;
      let last_item = arr[0];
      for (let i = 1; i 1) {
      longest_consecutive = 1;
      last_item = arr[i];
      }
      }
      return max_longest_consecutive;
      };

  • @shubhanshusharma7457
    @shubhanshusharma7457 ปีที่แล้ว +88

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

  • @sontujain7851
    @sontujain7851 ปีที่แล้ว +34

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

  • @lucygaming9726
    @lucygaming9726 9 หลายเดือนก่อน +24

    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 5 หลายเดือนก่อน +1

      how?can you give some hint?

  • @lingasodanapalli615
    @lingasodanapalli615 ปีที่แล้ว +82

    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 ปีที่แล้ว +5

      yes i think the same, it should be N^3

    • @sandipan_c
      @sandipan_c ปีที่แล้ว +7

      Correct O(N * N * N)

    • @alexmercer416
      @alexmercer416 10 หลายเดือนก่อน

      how? can u explain pls
      @@sandipan_c

    • @alexmercer416
      @alexmercer416 10 หลายเดือนก่อน

      how can u explain pls ?
      @@b_01_aditidonode43

    • @Rahul_Mongia
      @Rahul_Mongia 7 หลายเดือนก่อน +1

      no

  • @sumanthvarma1908
    @sumanthvarma1908 ปีที่แล้ว +75

    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 ปีที่แล้ว +9

      bhai wo zinda h

    • @khanra17
      @khanra17 11 หลายเดือนก่อน

      🤣🤣@@yowaimo890

  • @leoved1073
    @leoved1073 ปีที่แล้ว +37

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

  • @ritikshandilya7075
    @ritikshandilya7075 8 หลายเดือนก่อน +10

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

  • @takeUforward
    @takeUforward  ปีที่แล้ว +72

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

    • @leoved1073
      @leoved1073 ปีที่แล้ว +13

      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  ปีที่แล้ว +22

      Yess that was the reason

    • @dayashankarlakhotia4943
      @dayashankarlakhotia4943 ปีที่แล้ว +1

      Understood

    • @heybuddy_JAI_SHREE_RAM
      @heybuddy_JAI_SHREE_RAM ปีที่แล้ว +2

      Thank you brother for this DSA 💥💥course

  • @prabhagaikwad4849
    @prabhagaikwad4849 ปีที่แล้ว +34

    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 ปีที่แล้ว +5

      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 ปีที่แล้ว +7

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

    • @krishanubiswas6869
      @krishanubiswas6869 ปีที่แล้ว

      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 ปีที่แล้ว

      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

  • @yashraj5898
    @yashraj5898 3 หลายเดือนก่อน +4

    dont confuse guys when solving this just do else if (a[i] == a[i-1]) {
    continue;
    } to handle duplicate edge cases

  • @yourstrulysidhu
    @yourstrulysidhu ปีที่แล้ว +7

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

  • @infernogamer52
    @infernogamer52 ปีที่แล้ว +9

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

  • @CodewithKing360
    @CodewithKing360 6 หลายเดือนก่อน +10

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

  • @thebishalpaul
    @thebishalpaul ปีที่แล้ว +10

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

  • @RaunitJaiswal-s9v
    @RaunitJaiswal-s9v 3 หลายเดือนก่อน +1

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

    • @adityalawande1771
      @adityalawande1771 2 หลายเดือนก่อน

      Thank you soo much dude you really helped me where I was stuck !!!

  • @amanpreetsinghbawa1600
    @amanpreetsinghbawa1600 18 วันที่ผ่านมา

    The Sorting based approach got me a better percentile at LC, not sure about the Set based one(why it got a lower). Thanks Striver as always, posting this just for refs

  • @Bigg_boss_trolls
    @Bigg_boss_trolls 2 หลายเดือนก่อน

    Excellent course no one covered like this best way to teaching 🥰🤩

  • @many987
    @many987 ปีที่แล้ว +3

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

  • @trojanhorse8278
    @trojanhorse8278 6 หลายเดือนก่อน

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

  • @cinime
    @cinime ปีที่แล้ว +2

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

  • @tonylee1868
    @tonylee1868 ปีที่แล้ว +4

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

  • @Manishgupta200
    @Manishgupta200 ปีที่แล้ว +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

  • @iamnoob7593
    @iamnoob7593 5 หลายเดือนก่อน

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

  • @venkatraman7357
    @venkatraman7357 ปีที่แล้ว +2

    Understood. Amazing. Keep going mate.

  • @joelpappachan7853
    @joelpappachan7853 9 หลายเดือนก่อน

    You might be the GOAT of DSA

  • @RaunitJaiswal-s9v
    @RaunitJaiswal-s9v 3 หลายเดือนก่อน +2

    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

  • @shubhamagarwal1434
    @shubhamagarwal1434 4 หลายเดือนก่อน

    #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 4 หลายเดือนก่อน

      Say that again, after you discover something called “TUF+”

  • @harshukey6072
    @harshukey6072 ปีที่แล้ว +3

    Understood🎉
    40 lakh

  • @impalash_ag
    @impalash_ag 5 หลายเดือนก่อน

    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.

  • @striverdaaadi
    @striverdaaadi 11 หลายเดือนก่อน +1

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

  • @paritoshmalhotra6309
    @paritoshmalhotra6309 ปีที่แล้ว +12

    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 ปีที่แล้ว +3

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

    • @GOJOANDSUKUNAFAN
      @GOJOANDSUKUNAFAN 7 หลายเดือนก่อน

      ​@@calisthenics5247 yes correct I used hashing tho 😭

  • @pavansai2859
    @pavansai2859 9 หลายเดือนก่อน

    this is GOD level man!! Thank you!!

  • @anishaa3298
    @anishaa3298 หลายเดือนก่อน

    you are the GOAT. thank you

  • @meghasharma4875
    @meghasharma4875 4 หลายเดือนก่อน

    i directly got better approach in mind

  • @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)

  • @momentos811
    @momentos811 ปีที่แล้ว +1

    Your video is very helpful for everyone, thank you ❤

  • @lillyput2275
    @lillyput2275 4 หลายเดือนก่อน

    thanku so much striver ji ...loads of love

  • @vaibhavpratapsingh9956
    @vaibhavpratapsingh9956 ปีที่แล้ว +11

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

  • @kaichang8186
    @kaichang8186 3 หลายเดือนก่อน

    understood, thanks for the perfect explanation

  • @Dhruvbala
    @Dhruvbala ปีที่แล้ว

    Very well explained, brother. Thank you

  • @sachinboreoffl
    @sachinboreoffl 10 หลายเดือนก่อน +1

    Great Explanation

  • @vaibhavkotary3949
    @vaibhavkotary3949 ปีที่แล้ว +2

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

  • @ksankethkumar7223
    @ksankethkumar7223 ปีที่แล้ว +73

    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 ปีที่แล้ว +5

      Nice observation!

    • @RajeevCanDev
      @RajeevCanDev ปีที่แล้ว +2

      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 ปีที่แล้ว +3

      @@RajeevCanDev It will run in the loop even though it is user defined function and hence TC boils down to N^3

    • @RajeevCanDev
      @RajeevCanDev ปีที่แล้ว +1

      @@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 ปีที่แล้ว +1

      @@RajeevCanDev idk about that but the approach he mentioned is O(N^3)

  • @nikhild.20
    @nikhild.20 ปีที่แล้ว

    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 5 หลายเดือนก่อน

      can you explain me bro why we using " == st.end() " and " != st.end()" ...... he used in many problems but i don't understood......

  • @amansrivastava1232
    @amansrivastava1232 6 หลายเดือนก่อน

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

  • @karthikpaladugula5424
    @karthikpaladugula5424 หลายเดือนก่อน

    Amazing content ❤‍🔥

  • @kabilm8040
    @kabilm8040 4 หลายเดือนก่อน +2

    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

  • @Bigg_boss_trolls
    @Bigg_boss_trolls 2 หลายเดือนก่อน

    well explanaation bro it helps me lotttttttttttttttttttttt

  • @parasarora5869
    @parasarora5869 หลายเดือนก่อน

    Great one !! ❤

  • @akworld2739
    @akworld2739 ปีที่แล้ว +1

    plese any one can help me on optimal solution
    if(st.find(it-1)==st.end());
    19:31 how this condtion will execute

    • @yogeshpatidar8195
      @yogeshpatidar8195 4 หลายเดือนก่อน

      st.end() refers to the position just after the last element in the set and st.find(x) does not finds the value then it will also return the position just after the last element in the set .

  • @shubhamdhiman5602
    @shubhamdhiman5602 ปีที่แล้ว +2

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

  • @sukhpreetsingh5200
    @sukhpreetsingh5200 ปีที่แล้ว +2

    Amazing explanation❤️

  • @chaitanyabalanagu626
    @chaitanyabalanagu626 ปีที่แล้ว +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

  • @Metacognator
    @Metacognator ปีที่แล้ว +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

  • @kroax9720
    @kroax9720 4 หลายเดือนก่อน

    Life Changing 💕💕

  • @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 ปีที่แล้ว +1

      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

  • @mynameam234
    @mynameam234 4 วันที่ผ่านมา

    Hey Striver, I came up with this solution
    It takes only O(1) space to solve
    class Solution {
    public int longestConsecutive(int[] nums) {
    int n=nums.length;
    if(n

  • @balakrishnanr648
    @balakrishnanr648 ปีที่แล้ว +1

    us, really a good one in TC Explain

  • @AryanVerma-y3m
    @AryanVerma-y3m 4 หลายเดือนก่อน

    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.

  • @kajiazadali7738
    @kajiazadali7738 ปีที่แล้ว +7

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

  • @prabhakaran5542
    @prabhakaran5542 10 หลายเดือนก่อน +1

    Understood ❤

  • @prapti2385
    @prapti2385 6 หลายเดือนก่อน

    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?

  • @Srinivasssssss
    @Srinivasssssss 11 หลายเดือนก่อน

    Fantastic Explaination..!

  • @dhruvkathpal7610
    @dhruvkathpal7610 6 หลายเดือนก่อน

    Can we do this question like , firstly hashing the array by first finding size of hash array and then hashing it and then checking whether the hash number is zero or not for the consecutive ones and then counting the number of non zero hash numbers and storing the max count , will this be a better solution?

    • @prapti2385
      @prapti2385 6 หลายเดือนก่อน

      No, you can't. This would throw a time complexity error in my opinion as when the number is very large such large size of array is not possible.

  • @surbhirathore._
    @surbhirathore._ 6 หลายเดือนก่อน +1

    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 6 หลายเดือนก่อน +1

      bro it should be i

  • @culeforever5408
    @culeforever5408 ปีที่แล้ว +2

    understood 🎯

  • @RaunitJaiswal-s9v
    @RaunitJaiswal-s9v 3 หลายเดือนก่อน

    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

  • @ShivamSingh-gk3qu
    @ShivamSingh-gk3qu ปีที่แล้ว +1

    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

  • @RohitRaj-kr7ti
    @RohitRaj-kr7ti ปีที่แล้ว +1

    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 ปีที่แล้ว

      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.

  • @tusharsrivastava107
    @tusharsrivastava107 4 หลายเดือนก่อน +1

    Can we not make it easy by using Sorted Set in java ?

  • @HR-pz7ts
    @HR-pz7ts 8 หลายเดือนก่อน

    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

  • @abhaymandal4903
    @abhaymandal4903 ปีที่แล้ว

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

  • @mdfaizanmdfaizan6041
    @mdfaizanmdfaizan6041 4 หลายเดือนก่อน

    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

  • @Sharuthi727
    @Sharuthi727 4 หลายเดือนก่อน

    understood striver , thank you

  • @yogeshedekar6078
    @yogeshedekar6078 ปีที่แล้ว

    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 10 หลายเดือนก่อน

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

  • @Adityakumar-dl4vc
    @Adityakumar-dl4vc 2 หลายเดือนก่อน +1

    Thank ❤❤ you

  • @jagratgupta8392
    @jagratgupta8392 ปีที่แล้ว

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

  • @samuelfrank1369
    @samuelfrank1369 11 หลายเดือนก่อน

    Understood. Thanks a lot

  • @rahulsidhu5945
    @rahulsidhu5945 ปีที่แล้ว

    Hello everyone...
    I have a doubt in brute force solution which is given in the video. Please help me if anyone understand my doubt.
    For test case:
    5,8,3,2,1,4
    The longest subsequence will be 1,2,3,4,5 (len=5)
    But according to brute force solution when my i=4 ,it is pointing to the 1 in first for loop
    in second for loop we are trying to find out the 2,3,4,5 from the starting ..
    firstly we try to find out the 2 and then three.. but in our in test case 2 comes after the 3 so the 3 will skip.. and we never got the correct solution...
    This is code
    int longestSuccessiveElements(vector&a) {
    int count=1;
    int maxcount=1;
    for(int i=0;i

    • @hetanshumalik6778
      @hetanshumalik6778 ปีที่แล้ว +7

      A long comment but I have answered where you went wrong
      The brute force solution mentioned in the video has repeated searching of consecutive elements possible for an x. If x+1 has been found for an x we search for x+2 and x+3 and so on. What it means is that for a particular element (say x) picked up by the outer loop - for `i` in range(len(a)) [Bear with me for writing pythonic pseudocode], we look for `x+1` in the while(ls(arr,x+1)) part. Since it's a while loop, if `x+1` is found which leads to the true condition of while loop, the counter is incremented and (as linear search returns true), the x value has been increased to `x+1` i.e. the original value of x itself has been incremented. This is so because now the while loop would actually search for `x+1` (This x is the incremented x) which actually is `x+2` (Here x means original x we picked from the outer for loop). If again we find `x+2`, we look for `x+3` and so on. Do note that this brute force approach will have TC of O(n^3) as the linear search in while loop itself would take another O(n) and not O(n^2)
      The code you have written has the ability to pick an element `x` and only search for `x+1` as it has only two loops - the outer one picks the element x and the inner nested loop only looks for the `x+1`. Your code has no ability to look for `x+2` if it occurs before `x+1` as it has already been accessed by the inner loop and discarded. To actually search for `x+2` you need to go back and search once again. This code has only TC of O(n^2) and simply has no ability to repeatedly search for successive incremented x values which was achieved in the video's brute force solution by a while loop.

    • @sathya9922
      @sathya9922 5 หลายเดือนก่อน

      ​@@hetanshumalik6778 Thank u so much

  • @infernogamer52
    @infernogamer52 ปีที่แล้ว +1

    Understood Bhaiya!

  • @tharunalla617
    @tharunalla617 2 หลายเดือนก่อน

    Can we do it using map to get O(N) complexity?

  • @khanra17
    @khanra17 11 หลายเดือนก่อน

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

  • @kushagrakulshrestha2966
    @kushagrakulshrestha2966 2 หลายเดือนก่อน

    Hello, can you make a NEW playlist where you just code the java solutions of all these problems? we would get better understanding that reading the notes/codes directly and would have an easier time learning to code in java , you can attach those videos individually or just make a separate playlist and we will automatically go to that and see the java code after we are done understanding the algorithm

  • @sayantanpoddar5428
    @sayantanpoddar5428 ปีที่แล้ว

    understood
    please came with String playlist sir

  • @cameye9
    @cameye9 7 หลายเดือนก่อน

    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 7 หลายเดือนก่อน

      Kinda true only for this array

  • @dosapatiteja1588
    @dosapatiteja1588 11 หลายเดือนก่อน

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

  • @shineshine9269
    @shineshine9269 ปีที่แล้ว

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

  • @YourCodeVerse
    @YourCodeVerse ปีที่แล้ว

    Understood✅🔥🔥

  • @smilemka3342
    @smilemka3342 6 หลายเดือนก่อน

    set already stores in sorted and unique mannaer right?

  • @madhupadayeswanth1865
    @madhupadayeswanth1865 ปีที่แล้ว

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

  • @I_Keshav_Prajapati
    @I_Keshav_Prajapati ปีที่แล้ว +1

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

    • @himanshumewari6904
      @himanshumewari6904 ปีที่แล้ว

      no its unordered set

    • @KeepCoding69
      @KeepCoding69 ปีที่แล้ว +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.

  • @Abhay14
    @Abhay14 ปีที่แล้ว

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

  • @NazeerBashaShaik
    @NazeerBashaShaik 9 หลายเดือนก่อน

    Understood, thank you.

  • @MukulRattan
    @MukulRattan 3 หลายเดือนก่อน

    for the set solution. why he is not considering complexity of set.find() which is O(logn) ?

    • @unknown__899
      @unknown__899 3 หลายเดือนก่อน

      because it's converted into O(1) after some operations

  • @ADITYAMAHESHWARI-x5f
    @ADITYAMAHESHWARI-x5f 11 หลายเดือนก่อน

    Insertion in set is logn...so for n elements, isnt it o(nlogn) tc??

    • @GOJOANDSUKUNAFAN
      @GOJOANDSUKUNAFAN 7 หลายเดือนก่อน

      That's rare in most is o(1)

  • @sumitkawade9396
    @sumitkawade9396 ปีที่แล้ว +4

    Can anyone explain if(st.find(it-1)==st.end()) && if(st.find(x+1)!=st.end())

    • @himanshukaushik9223
      @himanshukaushik9223 ปีที่แล้ว +4

      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

  • @Leo-id4uh
    @Leo-id4uh 5 หลายเดือนก่อน

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

  • @darshantawte7435
    @darshantawte7435 ปีที่แล้ว

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

  • @shreekarpasupuletia2269
    @shreekarpasupuletia2269 4 หลายเดือนก่อน

    please write the ode for 1st and 2nd method also in coming videos