Find the duplicate number (LeetCode 287) | Full solution with different methods | Study Algorithms

แชร์
ฝัง
  • เผยแพร่เมื่อ 21 ก.ค. 2024
  • To see more videos like this, you can buy me a coffee: www.buymeacoffee.com/studyalg...
    You are given an array of integers, that have all unique values except one integer that may be duplicated more than 2 times. You just need to return this value. Seems like a very easy problem but it offers you a lot of scope. There can be several different solutions possible based upon the requirements. This video explores a brute force method, a time efficient method and a space efficient method. All along with easy to understand visuals and animations.
    Chapters:
    00:00 - Intro
    01:27 - Problem statement and description
    03:26 - Brute Force approach to find the duplicate number
    05:13 - Find the duplicate number using sorting
    07:07 - Find the duplicate number using a HashSet
    10:16 - Time and space efficient solution to find duplicate
    14:14 - Dry-run of code
    16:50 - Final Thoughts
    Actual problem on LeetCode: leetcode.com/problems/find-th...
    📚 Links to topics I talk about in the video:
    Detect Cycle Start in Linked List: • Linked List Cycle 2 (L...
    Linked List Introduction: • Linked List Data Struc...
    Brute Force Method: • Brute Force algorithms...
    Quick Sort: • Quick Sort super easy ...
    Time Complexity: • Big O Notation Simplif...
    Playlist on Linked Lists: • Linked Lists
    📘 A text based explanation is available at: studyalgorithms.com
    Code on Github: github.com/nikoo28/java-solut...
    Test-cases on Github: github.com/nikoo28/java-solut...
    📖 Reference Books:
    Starting Learn to Code: amzn.to/36pU0JO
    Favorite book to understand algorithms: amzn.to/39w3YLS
    Favorite book for data structures: amzn.to/3oAVBTk
    Get started for interview preparation: amzn.to/39ysbkJ
    🎥 My Recording Gear:
    Recording Light: amzn.to/3pAqh8O
    Microphone: amzn.to/2MCX7qU
    Recording Camera: amzn.to/3alg9Ky
    Tablet to sketch and draw: amzn.to/3pM6Bi4
    Surface Pen: amzn.to/3pv6tTs
    Laptop to edit videos: amzn.to/2LYpMqn
    💻 Get Social 💻
    Follow on Facebook at: / studyalgos
    Follow on Twitter at: / studyalgorithms
    Follow on Tumblr at: / studyalgos
    Subscribe to RSS feeds: studyalgorithms.com/feed/
    Join fan mail: eepurl.com/g9Dadv
    #leetcode #programming #linkedlists

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

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

    Your explanation is amazing. Thank you so much for putting so much effort into your video. : D

  • @user-pb5so8jx2k
    @user-pb5so8jx2k 7 วันที่ผ่านมา

    Bhai ka padhaya h...itne pyar aur patience se...thank you so much❤

    • @nikoo28
      @nikoo28  7 วันที่ผ่านมา

      glad it was helpful :)

  • @sameersayyad6170
    @sameersayyad6170 8 หลายเดือนก่อน +1

    brother the detailing in this video and your way of teaching is just mind blowing, until now i was feeling dsa is boring but this has just made me interested all over again. Thanks a ton ! Love!!

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

      thanks for the attention to detail :)

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

    The way you explain is amazing,,, keep posting more videos, Looking for more easy and medium leetcode problems

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

      Sure 👍

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

    really like your explanation style and diagrams!

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

    Pure GOLD ! Keep it up brother.

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

    I think you worth my time great explanation bro . Thanks for your efforts and guidance

  • @srikavyaswarna4122
    @srikavyaswarna4122 3 วันที่ผ่านมา

    Sir, your voice and clear cut communication skills are awesome...you have explained the driver code also.. with the original code...! Those who Dont know the coding.. they can also can understand.. So, thank you very much for providing this good quality videos on the youtube ..❤❤

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

    Awesome explain the last one was supper ... Yes we can do that with hashmap just store the count of value as value and index as value in hashmap and then iterated over array and check the count of value in hashmap is greater than 1 or not if yes , just return the array value. TC is O(n) and SC is O(n)..

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

    your way of explanation creates and makes interest to learn the solutions of the problem sir 👌

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

    Your explanation style is so good.

  • @user-kc4yv5kt3j
    @user-kc4yv5kt3j 9 หลายเดือนก่อน +1

    Great explanations. Thanks you!

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

    This is the best of all and I nearly spent 30 mins of time to search for a best one and I sticked with this . Thanks Brother btwn your explanation and presentation was perfect.🙌Keep teaching and sharing.

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

      Glad you liked it!

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

      There is another solution which is change the number to negative based on the index value. It's also o(n).

  • @VishalTrivediB
    @VishalTrivediB 2 ปีที่แล้ว

    Awesome explanation, thank you!

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

    The way you explain is just awesome.

    • @mohammadnazrulislam-iz2xc
      @mohammadnazrulislam-iz2xc 2 หลายเดือนก่อน

      hey bro can you please explain me ;
      how fast pointer moving two step and how slow pointer moving 1 step;
      according to code it can jump randomly.
      like at 14:15 if slow pointer pointing on 2nd index then in next loop slow pointer will point 4th index its just jump the 3rd index.

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

    can you please explain how to solve this using binary search

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

    but lets take [1,2,3,4,5,5] as the array ,, now slow =nums[0] which is 1 and fast=nums[nums[fast]] which is is 2 so slow is at sitting at first node and fast is siting at 2nd node . in this case now fast is two step ahead with slow ?

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

    a comprehensive solution 👍

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

    Everytime when the question says "Duplicate" I follow Hashset. But this Logic is better. Thanks

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

    I have found very helpful videos 🎉

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

    great explanation men

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

    Great job bro.

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

    With the link list solution what do you do when the element corresponds to an index out of bounds? Eg. 2,5,8,8,4.
    The problem statement only mentions "positive integers". It does not say anything about an upper bound for the elements?
    PS: im refering to the statement you showed in the video which may be different from Leetcode's original statement.

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

      this is a direct solution to the problem available on leetcode, so I would assume we are taking the same constraints.

  • @user-jr1vc3cc6c
    @user-jr1vc3cc6c ปีที่แล้ว +1

    amazing!!

  • @emma_anime
    @emma_anime 20 วันที่ผ่านมา

    thank you bro

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

    does the hashset solution have O(n) complexity as for each element we are also checking the set to check if it occured before in the set. as in the worst case if the repeating element occurs in the end of the array you have to insert n-1 element into the set and iterate through the set for all these n-1 elements then that tc would be about O(n2).

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

      The best case time complexity will be with the last method

  • @developerUtkarsh
    @developerUtkarsh 8 หลายเดือนก่อน +1

    Bhaiya isme sum of array and sum of number of elements n*n+1/2 and then subtract it we can get the answer but I got run time error how do I resolve it

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

      check the code in the video description. That works perfectly.

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

    Hi bro, the problem constraint is nums[I] of value 1 to n, why can't we achieve using xor operator

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

      XOR works when all numbers are duplicated, except one.
      example: you can XOR all numbers in array [ 1, 1, 5, 5, 7, 6, 7, 6, 4 ]
      The answer will be 4 in this case

  • @user-zh1ok2pf2z
    @user-zh1ok2pf2z ปีที่แล้ว +1

    perfect in all side

  • @honey-xr5kp
    @honey-xr5kp 3 หลายเดือนก่อน

    ahh this problem has me so dizzy. just starting with medium problems today. and using the array like its a linked list is just weird to me. but it works well it seems.

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

    Nikil, Suppose 3rd & 4th position is 25,25 , how will work based on index?

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

      Give me the complete test case

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

    One question: after you find the point slow and fast met, why are you setting the slow as 0 again? I feel like if you set slow as 0 and fast at the point where fast and slow meets, are they not supposed to meet with each other every again?

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

      Have a look at: th-cam.com/video/95ZfuoSAUPI/w-d-xo.htmlsi=fL17cmRkqyjattlJ
      This explains the mathematical proof

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

      From the cidoe you reference: we got the equation (n_2 - 2n_1)L = x + y. This equation alone indicates the spot when the fast and the slow meet together is special. On the right side, x+ y is the distance fast will travel. On the left side, (n_2 - 2n_1)L is the distance where the slow will travel because the slow is already in the loop and its traveling distance must be a constant times L. Am I correct? By the way, I appreciate that you responded to me that quickly. Thanks a ton. @@nikoo28

  • @AffairWithGeo
    @AffairWithGeo 11 หลายเดือนก่อน +3

    trick here is to think why in question it was actually said the number present in the array is 1 to N-1. then we can come up with this logic. Agar starting mein kuch aur hota 14 to lag jate is alogrithm ke, its not applicable

    • @nikoo28
      @nikoo28  11 หลายเดือนก่อน +2

      yep, these are the problem contraints

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

    What if the number is more than the index number than it says out of bound then how

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

      That will not happen with the given problem constraints

  • @lonen3rd
    @lonen3rd 9 หลายเดือนก่อน +1

    Is the hash set solution really O(n) time complexity? For every element, you have to check whether it's in the set, which takes O(n). Meaning it should be O(n*n) worst case.

    • @nikoo28
      @nikoo28  9 หลายเดือนก่อน +1

      the problem constraints avoid the worst case time complexity, check the leetcode link

  • @Satishsingh-yc9bs
    @Satishsingh-yc9bs 2 หลายเดือนก่อน

    So this optimized solution will work only when the numbers are in the range (1- n) ?

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

      That is the constraint of the problem

    • @Satishsingh-yc9bs
      @Satishsingh-yc9bs 2 หลายเดือนก่อน

      @@nikoo28 Thanks for clarification. But then this looks like a Trick question if the target to get this particular solution. Loving your videos by the way.❤

  • @user-yf9fo2pg5e
    @user-yf9fo2pg5e 7 วันที่ผ่านมา

    not allowed to use extra space, using linked list or hash map would not work

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

    Why we need to move the fast pointer twice of slow in the do while loop?

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

      as I say in the video...we use the concept of finding the cycle in a linked list, which works on the hare and tortoise algorithm. Find the link in the description to understand how that two pointer approach works. :)

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

      @@nikoo28 Thanks for the quick response.

  • @rudrajitpaul4088
    @rudrajitpaul4088 22 วันที่ผ่านมา

    i did not understand fast=nums[nums[fast]] .if nums[fast] is 3?

    • @nikoo28
      @nikoo28  22 วันที่ผ่านมา

      then fast = nums[3]

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

    Why do we need a slow pointer and a fast pointer? Would we not be able to reach the answer if we have both pointers going at the same speed and just see when they both meet? Essentially just doing the second portion of your code.

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

      just try an example and see how the pointers behave, I am sure you will get the answer yourself 😄
      the good old pen/paper works miracles

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

    Can anyone tells me that how this solution ends up in O(1) space complexity?
    Because, creation of linked list would take O(n) space?
    No?

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

      we did not create the linked list here, we just assumed the array to be a linked list, we did the linked list like operations on the array.
      here the ListNode can be assumed as the 'value' is the value of the element, and it's 'next' is actually the nums[value].

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

      Absolutely correct

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

      @@rishidangi2978 Okay got your point!

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

    Why is the repeated number guaranteed to be reachable from index 0?
    Why is it guaranteed to:
    1. Have a loop, which includes the repeated number?
    2. The repeated number starts the loop? Why it can't be in the middle of the loop?

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

      Both the questions are really good. I gave a reference to the original problem that explores the proof. Linked List Cycle 2.
      The link is available in the video description.
      th-cam.com/video/95ZfuoSAUPI/w-d-xo.html

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

      Let n be the size of the array. Assume all elements are 1 to n-1 inclusive. Starting from index 0, we construct a diagraph G in that manner. Every vertex in G has exactly one outgoing edge. Consider a walk from vertex 0. By the pigeonhole principle this walk will eventually reach a vertex for the second time, creating a directed cycle in G.
      QED

  • @kavinkavin3525
    @kavinkavin3525 20 วันที่ผ่านมา

    Impressive

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

    What if the value grater than array length?

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

      look at the problem constraints on leetcode

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

    The question mentions that You must solve the problem without modifying the array nums and uses only constant extra space, isn't sorting the array and using hashset violate this??

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

      If you look at the last method I talk about, that is the most efficient and does not modify the array. Uses constant space too. :)

  • @udayrajvadeghar8555
    @udayrajvadeghar8555 5 หลายเดือนก่อน +2

    what a solution bhiya

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

    your explanation is best,can you make a playlist of two pointers approach

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

      Two pointer approach for this problem or a two pointer approach in general??

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

      @@nikoo28general two pointer ,but not single video a playlist comprising all questions, because the two pointer playlist is not available on any channels

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

    Let’s say input was [2, 1] wouldn’t we go out of bounce after the first iteration?

    • @berzgar6927
      @berzgar6927 10 หลายเดือนก่อน +2

      there will be always a duplicate element and according to problem statement, in array of (n+1) length, elements will be from [0 , n]. So take any element from array(say n), we will have it in index also. Sorry for poor wording, but try to get a valid testcase and you will understand. (yours in not valid as array length is 2, so max element in it can be only 1 and obviously you don't have a duplicate in your array)

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

      @@berzgar6927 ah I See there will always be a dup. problem ranges from [1,n] inclusive btw

    • @nikoo28
      @nikoo28  9 หลายเดือนก่อน +1

      @berzgar6927 is absolutely correct

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

    Please explain the reason behind that "fast and slow" concept that you used.

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

      That is more or less based on finding a loop in a linked list. If you recall that problem, we apply the same concept over there.
      So as soon as the problem translates to a same structure, we apply the fast and slow pointer approach

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

      @@nikoo28 This algo is called Floyd's Tortoise. But I have found 2 problems with it.
      1. If the first element(0th index) is has 0. Then 0 will keep pointing to itself and will give us 0 as result.
      2. If two indexes have each other as their value. Then they will keep pointing to each other.
      For these two cases i am not getting the right answer. Please suggest something.

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

    Last wala while loop kyu lgya kuch clear nahi hua can u pls explain it here

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

      That loop will help the fast pointer to catch the slow pointer.
      This approach is borrowed from detecting a loop in a linked list. Please also watch that video…and everything will be definitely clear.

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

      Let me know if you still face issues

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

      Thank you very much bahiya not its 101% clear nd i am feeling good after successfully understand this and also thanks for replying 🙏👍

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

    u are awesome

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

    Heya! Great explaination as usual.
    I'm just having one concern, if I search for any problem on TH-cam which you have solved I don't get suggestion for you channel easily. Even after adding the end phrases as '..by study algorithms' I don't get the result. (I have subscribed you channel)
    Many of the times I have put the exact string which you have given as a title to the video or I have to search for channel then playlist and then video.
    One reason might be is it always suggest the videos with more views and another reason which I suspect is the name of the channel.
    Bcz others also put phrase like ' study algorithm' in their videos either in title or tags.
    My humble suggestion is to go digger into TH-cam algorithm and figure out something which will give us results (e.g adding all combinations of tags.)
    Your explanations are way better and I really want them to reach more n more learners. Thanks! 😊

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

      Yes I am also agree with with this point. I also don't get the results and suggestions to your videos easily.
      And I also think the name of the channel can be one reason because everyone puts 'study' / 'algorithms' in their videos.

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

      Thanks firstly for both of your suggestions 🙂. I am actually working on a new channel name that could help to uniquely identify results.
      Support from followers like you is really motivating and pushes me to work harder.
      Thanks once again, I will try to get those changes as soon as time permits.

  • @heyneisha3816
    @heyneisha3816 22 วันที่ผ่านมา

    Soooo I have watched this video multiple times and have looked at the question you show in the beginning of the video. Though I understand what you’re doing with using an index to identify duplicates what I don’t understand how this would work based off your question without knowing the range of numbers in the array. Your example uses something like this: [2,4,1,6,3,1]. How will this solution work with an array like this based off the question?[3,16,1,4,2,1].

    • @nikoo28
      @nikoo28  20 วันที่ผ่านมา

      read the problem statement again.
      "Given an array of integers nums containing n + 1 integers where each integer is in the range [1, n] inclusive."
      So for your test case when there is a 16, there should be atleast 16 elements in the array. Always read the problem constraints...they do give out a very good hint :)

    • @heyneisha3816
      @heyneisha3816 20 วันที่ผ่านมา

      @@nikoo28 where is this question in your video? Please reread the question you showed us in the video.

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

    We can also solve this using Binary Search

  • @travelnlearn
    @travelnlearn 2 ปีที่แล้ว

    nice

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

    if the element of the array is 0 to n or 1 to n we can do cyclic sort which takes O(n) time complexity and O(1) space complexity for more information on cyclic sort do check kunal kushawaha video on dsa java bootcamp

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

      any topics you got in mind?

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

    please make recursion playlist

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

      will do that soon

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

    its cool

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

    My Hashing technique got accepted on the leetcode
    the best optimal approach is lil bit tricky

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

      Sure, the hashing technique gets accepted…but thinking out different solutions never hurt. A good exercise for your brain 😄

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

      @@nikoo28 exactly 🤭

  • @shubhamsonar2930
    @shubhamsonar2930 2 ปีที่แล้ว

    amazing..keep going

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

    Nikhil bro 👌

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

    ye sochunga kaise main interview me?

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

      practice, practice and a lot of more practice.

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

      @@nikoo28 hello nikhil , that is true
      but i am SDE3 , and still feel kuchh questions trivial hi hain , agar nahi kiye to nahi honge.
      Do you agree?

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

      i wouldn't say that you will never be able to solve them. The thing is you will start identifying patterns...and after a while you will be able to realize if the problem is actually challenging or just tricky. Talking about an interview, if you just walk through the thought process and come up with all possible ways to attack, the interviewer will be happy...that you know all these approaches.
      So, coming back to your original question...if you find such a problem in an interview...don't just sit quiet and try to figure out a solution. Keep talking about all the approaches you have in mind...this will surely help the interviewer to assess you better. They can nudge you in the right direction.

  • @shishir-kon
    @shishir-kon 4 หลายเดือนก่อน

    16:26 I think you don't know what to do.
    You're saying we have to move the pointers to the beginning. Actually, you have to move only one of the pointers to the beginning.

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

      you are absolutely correct. Sorry for the mistake while recording...but glad you got the concept correct 🙂

  • @prabhavagrawal1712
    @prabhavagrawal1712 2 ปีที่แล้ว

    just xor all the numbers and xor again 1 ... n. You will get your ans

    • @nikoo28
      @nikoo28  2 ปีที่แล้ว

      Your solution will not work with an array like [1, 2, 2, 2, 2, 5]

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

    in Leetcode, this solution results in TLE

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

      checkout the solution I have in video description/dry-run of code. The code passes successfully :)

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

    i feel like the question is specifically made according to ur solution ...thank god i watched it way before my interview....not a good solution! Subscribed but then unsubscribed in the end

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

      suggest me a better approach please. :)

  • @syedshahzaibzafar2422
    @syedshahzaibzafar2422 2 ปีที่แล้ว

    your time and space efficient solution will fail. if some of the value of array is greater than array length

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

      read the problem statement correctly. the array integers nums containing n + 1 integers where each integer is in the range [1, n] inclusive.
      so there will never be a case when the integer is greater than the array size.

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

      With n we have a problem, that's already out of bounds.

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

      @@Grassmpl There's a constraint nums.length == n + 1. Meaning n is always < nums.length. So n will never be out of bounds.

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

    ujwal gamer zindabad