🔴 Amazon Interview Question - Missing Smallest Positive Number | Free Giveaway 🎁

Sdílet
Vložit
  • čas přidán 26. 07. 2024
  • ✅New CZcams Account - Developer Bhaiya 👉🏻bit.ly/developer-bhaiya-youtube 👀rachitiitr.com - My Personal Portfolio and things I recommend for Coding Interviews.
    Rachit, an ex-Software Engineer@Microsoft discuss in detail the initial ideas, the correct strategy, the code and time/space complexity analysis of a Greedy problem on Arrays.
    Problem Link (for you to practise): leetcode.com/problems/first-m...
    Github Code in Python (Star the repository): github.com/rachitiitr/DataStr...
    Github Code in C++ (Star the repository): github.com/rachitiitr/DataStr...
    ✅ Complete Playlist "FANG Coding Interviews with Animated Solutions" 👉🏻 • Amazon Interview Quest...
    ✅ 𝗘𝗱𝘂𝗰𝗮𝘁𝗶𝘃𝗲.𝗶𝗼 [10% OFF for First 70 Users] 👉🏻educative.io/rachit
    ⚠️Rules for 3-month giveaway to Educative website
    👉🏻Follow @rachitiitr on Instagram
    👉🏻Comment below on this CZcams video, "interested @your_instagram_id_here"
    👉🏻A random winner from comment would be decided in 4 weeks
    👉🏻The Winner gets contacted on their Instagram with the voucher for giveaway
    𝗜𝗡𝗧𝗘𝗥𝗩𝗜𝗘𝗪 𝗣𝗥𝗘𝗣 𝗣𝗥𝗢𝗗𝗨𝗖𝗧𝗦
    ✅ 𝗔𝗹𝗴𝗼𝗘𝘅𝗽𝗲𝗿𝘁 [15% OFF coupon "rachit"] 👉🏻algoexpert.io/rachit
    ✅ 𝗦𝘆𝘀𝘁𝗲𝗺 𝗗𝗲𝘀𝗶𝗴𝗻 [Discount for Indian audience] 👉🏻bit.ly/design-rachit
    ✅𝗘𝗱𝘂𝗰𝗮𝘁𝗶𝘃𝗲.𝗶𝗼 [10% OFF for First 70 Users] 👉🏻educative.io/rachit
    ✅ 𝗣𝗿𝗼𝗴𝗿𝗮𝗺𝗺𝗶𝗻𝗴 𝗕𝗼𝗼𝗸𝘀 [Amazon Affiliate] 👉🏻amazon.in/shop/rachitjain
    SUBSCRIBE AND HIT BELL ICON TO CHECK MORE OF MY CONTENT
    czcams.com/users/RachitJain?sub_con...
    𝗜𝗠𝗣𝗢𝗥𝗧𝗔𝗡𝗧 𝗣𝗟𝗔𝗬𝗟𝗜𝗦𝗧𝗦
    ✅ 𝗖𝗼𝗱𝗶𝗻𝗴 𝗜𝗻𝘁𝗲𝗿𝘃𝗶𝗲𝘄 𝗟𝗲𝗰𝘁𝘂𝗿𝗲𝘀 👉🏻 • Day 3: Coding Intervie...
    ✅ 𝗚𝗿𝗮𝗽𝗵 𝗧𝗵𝗲𝗼𝗿𝘆 𝗣𝗹𝗮𝘆𝗹𝗶𝘀𝘁 👉🏻 • Graph Theory and Algor...
    ✅ 𝗖++ 𝗦𝗧𝗟 𝗣𝗹𝗮𝘆𝗹𝗶𝘀𝘁 👉🏻 • The Best Demo on C++ S...
    ✅ 𝗠𝘆 𝗣𝗲𝗿𝘀𝗼𝗻𝗮𝗹 𝗜𝗻𝘁𝗲𝗿𝘃𝗶𝗲𝘄 𝗘𝘅𝗽𝗲𝗿𝗶𝗲𝗻𝗰𝗲𝘀 👉🏻 • Uber SDE II Interview ...
    ✅𝗣𝗿𝗼𝗱𝘂𝗰𝘁𝗶𝘃𝗶𝘁𝘆 𝗧𝗶𝗽𝘀 𝗣𝗹𝗮𝘆𝗹𝗶𝘀𝘁 👉🏻 • Uber SDE II Interview ...
    ✅𝗟𝗶𝗳𝗲 𝗟𝗲𝘀𝘀𝗼𝗻𝘀 & 𝗠𝗲𝗻𝘁𝗼𝗿𝘀𝗵𝗶𝗽 👉🏻 • Why I Chose To Become ...
    𝗦𝗢𝗖𝗜𝗔𝗟 𝗣𝗥𝗢𝗙𝗜𝗟𝗘𝗦
    ✅rachitiitr.com
    ✅ / rachitiitr
    ✅ / rachitiitr
    AlgorithmsWithRachitJain
    ✅ / rachitiitr
    𝗣𝗥𝗢𝗚𝗥𝗔𝗠𝗠𝗜𝗡𝗚 𝗣𝗥𝗢𝗙𝗜𝗟𝗘𝗦
    ✅ Github ► github.com/rachitiitr/DataStr...
    ✅ Programming Blog ► rachitiitr.blogspot.com
    ✅ CodeForces ► www.codeforces.com/profile/rac...
    ✅ CodeChef ► www.codechef.com/users/rachitiitr

Komentáře • 585

  • @sjchat
    @sjchat Před 4 lety +169

    the same question they asked me in round 2 of sde 2. I gave an answer of O(N), then suddenly they wanted an answer without extra spaces!! I stucked forever there. 😶

    • @krsingh.shubham
      @krsingh.shubham Před 4 lety +10

      So you got accepted or not ? XD

    • @sjchat
      @sjchat Před 4 lety +18

      @@krsingh.shubham In this particular round, they was ok with me, I got rejected from Managerial round

    • @krsingh.shubham
      @krsingh.shubham Před 4 lety +12

      @@sjchat aah !. good luck other interviews.🤞

    • @sjchat
      @sjchat Před 4 lety +3

      @@krsingh.shubham thanks dude. ❤️

    • @sohamsen7892
      @sohamsen7892 Před 4 lety +13

      @@sjchat That's great. At least you reached the managerial round. I had my heart in my mouth when you said you couldn't come up with constant space solution.

  • @karthicksridhar840
    @karthicksridhar840 Před 4 lety +82

    I guess now i am able to solve leetcode's first missing positive hard question. No resource did this easy explanation. Thanks Rachit.

    • @jay-rathod-01
      @jay-rathod-01 Před 3 lety +1

      thats true. but i wanna know how long do they take to design and make this algorithm work.

  • @rutuja7293
    @rutuja7293 Před 4 lety +21

    This question was really cool. Amazing solution !

  • @EndvrX
    @EndvrX Před 2 lety +6

    I was asked this as my first question on my Round 1 of Amazon interview and I gave the O(max(element)) soln and he was satisfied. He asked me to optimize it further but was not able to do it. Thanks Rachit for doing such a wonderful solution.

  • @sushmareddy1370
    @sushmareddy1370 Před 4 lety +3

    The fact that answer is

  • @vedkorla300
    @vedkorla300 Před 2 lety +6

    Dude, the whole animation and production is amazing along with the explanation.

  • @HarshGangwar
    @HarshGangwar Před 4 lety +17

    We can apply the first approach on -ve elements as well by using shifting. shift all -ve values to front and then apply the 1st approach you mentioned for +ve numbers in array.

    • @rohankrishnaullas6705
      @rohankrishnaullas6705 Před 4 lety +2

      That will change runtime to O(n^2).

    • @HarshGangwar
      @HarshGangwar Před 4 lety +1

      @@rohankrishnaullas6705 O(n) + O(m) m-> number of positive elements m

    • @rohankrishnaullas6705
      @rohankrishnaullas6705 Před 4 lety +1

      @@HarshGangwar consider an array of size n with fist n/2 elements positive and last n/2 elements negative. So to move all the last n/2 elements to front ,each of the last n/2 elements will have to traverse a distance of n/2 i.e O(n/2 * n/2) = O(n^2).
      Eg : [1,2,3,4....500,-1,-2,-3...-500]
      To move -1 to pos 0 takes n/2 ops and similarly for all rem negative elements.

    • @BarkaDog
      @BarkaDog Před 4 lety +2

      @@rohankrishnaullas6705 the approach you are explaining is shifting. You can just swap every negative element you encounter to the jth index(initially 0) and then increment the value of j. This will be O(n)

    • @akshaygodase8067
      @akshaygodase8067 Před 3 lety +1

      Instead, mark negative elements can be changed to zero and ignore them in the next traversal!

  • @arnavkarforma3015
    @arnavkarforma3015 Před 3 lety +6

    for anyone looking for a bit simpler code, with the same idea
    while(i < A.length){
    if(A[i] == i+1 || A[i] A.length) i++;
    else if(A[A[i]-1] != A[i]) swap(A, i, A[i]-1);
    else i++;
    }
    i = 0;
    while(i < A.length && A[i] == i+1) i++;
    return i+1;

  • @abhishekshankar1136
    @abhishekshankar1136 Před 4 lety +18

    This was asked in samsung and VMware interview too

  • @mitulmunjal3379
    @mitulmunjal3379 Před 4 lety +2

    In addition to the first approach, we can change the 0s and negatives to Array Size + 1 and then iterate in the array to make the postive numbers negative. After that, we can return the index (i+1 actually) of the first negative number found.

  • @rituparnakhaund54
    @rituparnakhaund54 Před 4 lety +5

    The while loop logic didn't cross my mind while implementation , now I got it . Thanks a lot.

    • @Mercer80
      @Mercer80 Před 2 lety

      why while loop is used ???

  • @soumyadeepghosh5043
    @soumyadeepghosh5043 Před 4 lety +9

    I just changed all negative number to n+1, but thanks to You I got another way to think about the solution. Thanks 😊

    • @snlagr
      @snlagr Před 3 lety +2

      yes your approach is more intuitive

    • @himanshusomani7
      @himanshusomani7 Před 3 lety +2

      alll negative and zero also then first method also works

  • @abhishekvishwakarma9045
    @abhishekvishwakarma9045 Před 4 lety +18

    When I started the video and think of the solution I got the XOR solution but then you threw the O(1) limit then I got stuck😅😅, As always Amazing video Rachit 😎🔥🔥

  • @MANIDEEP08
    @MANIDEEP08 Před 3 lety

    This is the simplest and beautiful solution.
    Thanks man!

  • @arihannnt
    @arihannnt Před 4 lety +6

    Amazing question ! 👌 And amazing solution ! 💯

  • @animatoworldsharmapooja9996

    A very good question explained in simplest way.Thank you sir...🙌

  • @minnamreddyvenkataprasanth5959

    Thanks Rachith Bhayya . I was really shocked why geeks was using this technique.... But u made me understand it very well♥️♥️

  • @imranwahid9871
    @imranwahid9871 Před 3 lety +3

    Best explanation ever on this question. Thanks a lot.

  • @manas_singh
    @manas_singh Před 4 lety +6

    This solution was amazing. I saw a question where the array was sorted and we had to find the mex in log(n) time. I guess we can simply find the point where difference between index and the number is 1 using binary search

    • @shobhitgupta3885
      @shobhitgupta3885 Před 3 lety +1

      Yes, we can use binary search. I think it can be done like this -
      Lets set l = 1 and r = n+1 for binary search. Then the number we predict as our ans will be mid. Then if the index of number is having number larger than it then our ans might be this mid and since we need to minimize we shift r towards mid. If the index equals the value then we should move our l towards mid. During the process we can keep track of minimum and get our ans

  • @shreekrishna6144
    @shreekrishna6144 Před 4 lety +1

    This is the bucketing technique.Thanks again man!

  • @competitivedoritos4294

    love your animated vids bhaiyya, it's really fun watching them!!!

  • @shvmsh20
    @shvmsh20 Před 3 lety +1

    This is best explanation of this question in the whole world. Thanks Rachit.

  • @rohanseth1392
    @rohanseth1392 Před 4 lety

    Thanks for the new approach Rachit. I was not able to get how time complexity is still O(N). I understood the whole solution and indeed it was very well explained.
    Thanks Rachit

  • @CRTagadiya
    @CRTagadiya Před 3 lety

    Didn't understand in GFG, then I suddenly remember Rachit has uploaded this, I saw in the notification a few days back but didn't watch at that time. and I got it .

  • @shaikhubaidahmed3609
    @shaikhubaidahmed3609 Před 3 lety

    thanks bhaiya for this solution what i couldn't do understand in 12 hr you made it understand in 14 min.god bless u

  • @maheswaran6628
    @maheswaran6628 Před 2 lety

    thank you for this simple and clear explanation brother

  • @manojjain3501
    @manojjain3501 Před 2 lety

    Jain Sahab , Tusi Chha Gaye .. Loved the explanation and Logic

  • @developer-world9040
    @developer-world9040 Před 3 lety

    Bhaiya u made it so simple now it's not looking hard one

  • @aakarshitrekhi8071
    @aakarshitrekhi8071 Před 3 lety

    multiplying with -1 approach is also constant space and u can add a larger number than n in place of negative numbers only there'll be one extra iteration

  • @nehamodak9660
    @nehamodak9660 Před 4 lety +1

    As the array can be modified then all the "non positive" numbers can be replaced with N+1 (N=size of the array). Then I think the previous approach will work.

  • @sridatta9650
    @sridatta9650 Před 4 lety

    Super, thanks for the pristine tutorial

  • @rahulsrivastava4811
    @rahulsrivastava4811 Před 4 lety

    Loved the way of teaching!!

  • @sagarkrishnabose8612
    @sagarkrishnabose8612 Před 4 lety +1

    We could have used Negative array indexing. O(n) TC and SC is O(1)

  • @aakashsharma5901
    @aakashsharma5901 Před 3 lety

    Too simple and great explanation bro

  • @RohitPatil_Tech
    @RohitPatil_Tech Před 4 lety

    Hey Rachit,
    Nice explanation. Thanks 🙂

  • @_U_will_always_shine
    @_U_will_always_shine Před 3 lety +2

    Oh my god. Hats off to the person who got with this solution. As a fellow developer the logic is amazing

  • @ganeshchowdhary3024
    @ganeshchowdhary3024 Před 4 lety +2

    To avoid failure of your O(1) space approach we can change all the negatives to 1 before applying that method. That would definitely work

  • @sohamsen7892
    @sohamsen7892 Před 4 lety +1

    Very helpful tip. How do you come up with such solutions, experience in competitive programming or some invisible tutorials??

  • @mohanramakrishna4824
    @mohanramakrishna4824 Před 4 lety +1

    Good explanation.Thank You

  • @ianku
    @ianku Před 2 lety

    When I was watching this video I also came up with an approach that if size is N and there is no copy then
    at max N+1 could be the mex
    so we can store the sum of N numbers let tmp = (N+1)*(N)/2
    and now iterate over the entire array if the value is less than N and greater then 0 decrease the value of tmp with that value ,
    after iteration if the tmp is 0 ans should be N+1 else ans will be tmp

  • @aditikaushik7350
    @aditikaushik7350 Před 3 lety

    Thanks a lot! Amazing solution!

  • @yashSharma-pe1dp
    @yashSharma-pe1dp Před 4 lety

    Before this i only knew that we can use the indexing trick for finding the missing and repeated number and counts of all the numbers when they are N total with no. ranging from 1 -N OR 0-N-1, Today i got something dangerous than this, following the same trick. Thanks Rachit.

  • @surajtopal9940
    @surajtopal9940 Před 3 lety

    thanks a lot brother to make tricky question easy

  • @hemantjain8777
    @hemantjain8777 Před 2 lety

    In this solution, we also need to make sure to break the loop if element at the current index remains the same after an iteration.

  • @utkarshgupta213
    @utkarshgupta213 Před 4 lety

    I think the second approach can be extended for negative no. too. Instead of multiplying by -1 we can subtract/add infinite to the value

    • @rishabjain2634
      @rishabjain2634 Před 2 lety

      @utkrash Gupta can u send the code buddy! n explaintion

  • @charan775
    @charan775 Před 4 lety

    we can make all -ve and > N numbers as 0 and rest all numbers occurrence will be marked as 1 in that index. and the first index which has 0 that is the missing

  • @DHWANIL19
    @DHWANIL19 Před 4 lety

    If I sort the array in ascending order like 1,3,4,7. Then iterate to check the difference between consecutive digits(3-1,4-3,7-4 etc). For the first difference that is >1, I add 1 to the left hand side value(1+1), to get the smallest number.

  • @AmanRaj-gy6qv
    @AmanRaj-gy6qv Před 2 lety

    Understood well, thanks for the help ,commeting on the video for better reach, cuz that's the only thing i could do for now😇

  • @03_abhishekchoubey42
    @03_abhishekchoubey42 Před 2 lety

    This question is also done as
    Int k=1
    For (int I=0;I

  • @GujjuMindset
    @GujjuMindset Před 3 lety

    Rachit Bhaiya!!! can you please make more animated videos like this of most asked interview quetions these are really very helpfull....!!!!

  • @MadForCs16
    @MadForCs16 Před 3 lety

    another solution would be :
    1) iterate through the array and replace the values which are

  • @jigyasakodnani3872
    @jigyasakodnani3872 Před 3 lety

    Amazing explanation !!

  • @electra_
    @electra_ Před rokem

    I will say that the question should probably specify whether the array is immutable, mutable (and if changing the order is ok), or consumed via the operation.
    If array is immutable, or if it must be returned to its starting state after the operation, then this solution will not work since there's no way to reverse the mixing-up.
    If array needs only keep the same values, this solution is fine (with MEX order doesnt matter so this is ok, but say you also had queries that indexed the array, this would be an issue)
    if array can be consumed by the operation, there is another way which is the one I thought of - to mark an element as seen, you replace that spot with a flag like 0 or -1, and then process whatever used to be there. Before this starts you can process elements replacing any that started out as negative with something else such that it won't be confused, like turning any -1 into -2.
    I figured that the operation would probably either want the array to be unchanged or let it be consumed, and so while i had thought of re-ordering the array while doing some pointer jumps, decided there would be no way to reverse the operation and so didn't follow that path.

  • @amitharitwal9378
    @amitharitwal9378 Před 4 lety +1

    In the second approach we can use -INF

  • @dhanubhardwaj6935
    @dhanubhardwaj6935 Před 4 lety

    I think first approach still work if intially we separate a negative no. and positive no. and did same with start from first positive integer.

  • @suchismitagoswami5609

    The problem with this approach is that to avoid the use of additional space, we are mutating the array, and it's difficult to reverse the change once done.

  • @shri9229
    @shri9229 Před 2 lety

    This exact approach gave me tle

  • @dileepmenon1948
    @dileepmenon1948 Před 4 lety

    For the case of all +ve numbers, if the array had repetitions say, arr =[1, 1], arr will become [-1,1] first and then [1,1] on multiplying by -1. We will get an answer as 1 when it should be 2. So multiplying by -1 should happen only when the element at that index is positive.

    • @Yeager098
      @Yeager098 Před 4 lety

      yes, u r right about that!!

  • @prasannanivas4575
    @prasannanivas4575 Před 4 lety +11

    The same code in python runs timeout in interviewbit

    • @utkarshagarwal8390
      @utkarshagarwal8390 Před 3 lety

      That's why Python isn't preferred for CP ... owing to more time it takes

    • @PhoenixRisingFromAshes471
      @PhoenixRisingFromAshes471 Před 3 lety +1

      @@utkarshagarwal8390 no...there is a problem in this logic..it will fail for duplicates

    • @PhoenixRisingFromAshes471
      @PhoenixRisingFromAshes471 Před 3 lety

      To solve this segeregate the negative from positives ..keep your negativity to left part of array and then apply the first algo taught in this video on the positive part

    • @dhruvpurwar6642
      @dhruvpurwar6642 Před 3 lety

      @@PhoenixRisingFromAshes471 r u sure? Coz there is a condition in while loop to check that?

  • @prashnought
    @prashnought Před 4 lety

    Also, why not have a current and current-1 iterator (here i) and check if the difference of data at these cells is greater than 1 and hence return the required value.
    Also, if for the current approach what if there are duplicate elements in the array?

  • @mayankverma7883
    @mayankverma7883 Před 4 lety

    Amazing solution sir

  • @sasmitshubham9424
    @sasmitshubham9424 Před 2 lety

    I was able to crack the solution after watching half part of the video, really excited to watch the next half of the video.

  • @miteshkumar3183
    @miteshkumar3183 Před 4 lety

    This is an easy problem. If you have done the problem where you must reorganize all numbers in an array A such that A[i] = A[i] for all i in O(1) space O(n) time, then this is significantly more straight forward to approach.

  • @lasue7244
    @lasue7244 Před 4 lety

    An easy solution would be using a sort function that doesn't use extra space. And maintain a extra variable 'a'. Every time negative and 0 entries are encountered, do a++ and move ahead in array. And compare array elements are[i] with i+1-a . In this way we basically ignore negative and zeroes and only compare +ive entries. If at any 'i' above comparison return false, I+1-a is missing

    • @balu.92
      @balu.92 Před 2 lety

      Only problem is time complexity, which will be O( n log n). Pretty sure you'll be asked to optimise it.

  • @jatinsharma1915
    @jatinsharma1915 Před 2 lety

    Here some people are saying that this code fails for repeated values, but this is false. The condition - num[i] != num[correctPos] checks this thing. If you encountered a number which is not at its correct position, and the correct position is already occupied by a correct number, then this condition will not allow infinite swapping of num[i] and num[correctPos].
    But if you replace the condition in while loop by i != correctPos, then in this case there will be infinite swapping in case of repeated numbers (once any one instance of the number gets placed in the correct position), because before swapping if wont check that the number num[i] that you want to swap with num[correctPos] are already same.
    But yes, the previous approach which has only positive numbers will fail in case there are repeated numbers. Suppose there are two 3s, then arr[3] will be multiplied by -1 two times, eventually making arr[3] positive.

  • @mthaha2735
    @mthaha2735 Před 3 lety

    Instead of negating the number. Convert the number to string. So on which index the number isn't a string is the least missing number

  • @vyshnavramesh9305
    @vyshnavramesh9305 Před 3 lety +1

    Pattern: Mark visited elements in O(N) time and O(1) space.
    Aproaches: Negate indices of values, Or swap values into correct indices
    Observation: First Missing Positive lies in between 1 and n + 1. Draw test cases to observe this.

  • @GGxEpsilon
    @GGxEpsilon Před 4 lety

    Does this work?
    What if we check the array once for '1', if it doesn't have a '1' we output the answer as one, if it has a '1' we set all negative numbers to 1 and proceed with the negative marking algorithm?

  • @divyarajsinhrana2094
    @divyarajsinhrana2094 Před 3 lety +1

    Can’t we sort the array and run the loop from i=0 to max_ele_in_arr and use count function. If count of that element is 0 then we return that variable.. Is this approach correct???

  • @srijansinha7732
    @srijansinha7732 Před 3 lety +2

    What if the array is 3 1 3 4 your swapping logic will stuck in loop swapping 3 and 3 repeatedly?

    • @gollasaivenkatesh
      @gollasaivenkatesh Před 3 lety

      you can write a logic that breaks out of a loop when value of element doesnot change after swapping

  • @prabhatrai6292
    @prabhatrai6292 Před 3 lety

    we can handel -ve value and zro by converting them to n+1 .

  • @kanhaiyasharma5834
    @kanhaiyasharma5834 Před 3 lety +2

    will it work for repeating integer values in the array (1,2,2,3,3,-2,-4,-5)
    or firstly we have to remove -ve and repeated elements from the array.

    • @gollasaivenkatesh
      @gollasaivenkatesh Před 3 lety

      you can write a logic that breaks out of a loop when value of element doesnot change after swapping

  • @abhi-5783
    @abhi-5783 Před 4 lety

    If an array has negative numbers we can shift all negative numbers to end of the array in O(n) time. Then we proceed with the marking negative approach

  • @lawbindpandey402
    @lawbindpandey402 Před 4 lety

    Cool !!

  • @pallavikankure9698
    @pallavikankure9698 Před 2 lety +3

    Great explanation, but fails for the array with repeated numbers, for ex. [1, 3, 6, 4, 1, -2]. As it swaps with the element at correct position, it enters infinite loop on the 2nd occurrence of 1.

    • @priyanshumishra4080
      @priyanshumishra4080 Před 2 lety +1

      exactly!! did u find which solution would be correct for those situations?

  • @bhavin_ch
    @bhavin_ch Před 4 lety

    What if you just deleted the number at ith index instead of swapping? Then won't index corresponding to the first non-null number be the ans?

  • @ashishchoksi8501
    @ashishchoksi8501 Před 4 lety +1

    if numbers are same then we need to small change in code that if(A[i]==A[A[i]-1]) break;

  • @raghibanwar826
    @raghibanwar826 Před 4 lety

    Sir we can also change the value of negative numbers or zeros to a number greater then n and then apply the first method. Correct me if I am wrong 😁.

  • @neghatnazir1668
    @neghatnazir1668 Před 4 lety

    awesome explination

  • @parashararamesh4252
    @parashararamesh4252 Před 4 lety

    Another solution can be introducing an N bit binary number where for every positive number we switch on the ith bit from the left. Then the index of the first switched off bit in the binary number is the answer

    • @BarkaDog
      @BarkaDog Před 4 lety

      What if there is a number like 1 lakh. You will take a 1 lakh bit binary number for it?

  • @adipratapsinghaps
    @adipratapsinghaps Před 4 lety +4

    Can such problems even be solved? These are just solutions that you either know, or you don't. How are these interview questions anyway? Crazy.

  • @ganeshchowdhary3024
    @ganeshchowdhary3024 Před 4 lety

    We can also use unordered_set and insert all the elements into it. Then iterate from 1 to N+1 and return which is missing

  • @harshvardhansingh780
    @harshvardhansingh780 Před 2 lety

    Nice explanation

  • @vinit472
    @vinit472 Před 4 lety +1

    Rachit I think your solution for the negative number may run in O(n2) in worst case scenario. Please correct me if I am wrong.

  • @toxicdesire8811
    @toxicdesire8811 Před 4 lety

    Will it be possible to cover a little more formal proof for correctness, for greedy ones please?

  • @bismeetsingh352
    @bismeetsingh352 Před 3 lety

    The time complexity is O(n^2).

  • @prateekmishra6245
    @prateekmishra6245 Před 3 lety

    Many have just copy pasted your code on leetcode :)

  • @awalktoinfinity665
    @awalktoinfinity665 Před 4 lety +3

    sir pls tell when the array will contain 0, how do we apply this method to find the missing no

    • @dipeshpatil2771
      @dipeshpatil2771 Před 3 lety

      The algorithm works for 0 also. Do check it yourself

  • @mohitjoshi4209
    @mohitjoshi4209 Před 4 lety +1

    @iknowsomedeeplearning .... Also connected with you in LinkedIn ... Really need this giveaway ..

  • @yuvrajbansal6294
    @yuvrajbansal6294 Před 3 lety

    Best explanation

  • @sammyshm
    @sammyshm Před 4 lety +1

    Will it goes to o(n ^ 2) in the worst case?
    One traversal for o(n) and inside that we are doing swap which could be for n -1 elements?
    Example array = [3, 4, 2, 9]
    at 10:50 you say same

  • @satyampatidar5848
    @satyampatidar5848 Před 4 lety +6

    Yesterday I solve this question in geekforgeeks...
    I can say this is very good question

  • @shubhamdebnath714
    @shubhamdebnath714 Před 4 lety

    here's another similar idea, do dfs just like Rachit did, but instead of marking negative, make it equal to its index , but beforehand save prev value to be used as next index for iteration, (handle any index less than 1 or greater than n by thinking them of as end of your dfs, another end will be when value is already been made equal to its index), now we just need to search for first element which doesn't match its index

    • @shubhamdebnath714
      @shubhamdebnath714 Před 4 lety

      my bad, didnt see ahead in the video before commenting, i just came here after solving it on leetcode , too much time taken

    • @shubhamdebnath714
      @shubhamdebnath714 Před 4 lety

      sorry for this long thread, you can not simply swap elements, i did try that, fails for cases like [1, 1] where swapping will create infinite loops

  • @awarahuman3312
    @awarahuman3312 Před 3 lety

    why are we returning n+1,f we are not getting any missing number

  • @nammi895
    @nammi895 Před 2 lety

    6:01 we you said paise the video and think it.
    I got a youtube add of algoexpert 🤣🤣

  • @SouravendraKrishnaDeb
    @SouravendraKrishnaDeb Před 4 lety

    whoa what a solution man

  • @swatibahl7799
    @swatibahl7799 Před 4 lety

    How about moving from 0 to length of array and searching for first missing element? I'm new to time-space analysis please spare me if I'm wrong.

  • @nikhilneela
    @nikhilneela Před 4 lety

    Why not we simply replace all negative and zeros with n+1 where n is the size of the array. Effectively we are not "touching" negative and zeros at all. Is there anything wrong with this approach ?

  • @vedavyas5953
    @vedavyas5953 Před 3 lety

    Here"s my approach I will start with i=1 and check if it's in the given array if not I will increment i it goes until i is not found. time complexity will be O(n)

    • @imshubham31
      @imshubham31 Před 3 lety

      Nope, you are neglecting the time taken by the search operation which is O(n). So O(n*n) overall.

  • @sevarth9263
    @sevarth9263 Před 3 lety

    What if we have duplicates in array (multiple occurrence of a number) will this approach work properly ?