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

Поділитися
Вставка
  • Опубліковано 2 лют 2025

КОМЕНТАРІ • 583

  • @sjchat
    @sjchat 4 роки тому +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 4 роки тому +10

      So you got accepted or not ? XD

    • @sjchat
      @sjchat 4 роки тому +17

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

    • @krsingh.shubham
      @krsingh.shubham 4 роки тому +12

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

    • @sjchat
      @sjchat 4 роки тому +3

      @@krsingh.shubham thanks dude. ❤️

    • @sohamsen7892
      @sohamsen7892 4 роки тому +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 4 роки тому +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 4 роки тому +1

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

  • @EndvrX
    @EndvrX 3 роки тому +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.

  • @HarshGangwar
    @HarshGangwar 4 роки тому +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 4 роки тому +2

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

    • @HarshGangwar
      @HarshGangwar 4 роки тому +1

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

    • @rohankrishnaullas6705
      @rohankrishnaullas6705 4 роки тому +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 4 роки тому +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 3 роки тому +1

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

  • @arnavkarforma3015
    @arnavkarforma3015 4 роки тому +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;

  • @vedkorla300
    @vedkorla300 3 роки тому +6

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

  • @sushmareddy1370
    @sushmareddy1370 4 роки тому +3

    The fact that answer is

  • @rutuja7293
    @rutuja7293 4 роки тому +21

    This question was really cool. Amazing solution !

  • @soumyadeepghosh5043
    @soumyadeepghosh5043 4 роки тому +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 4 роки тому +2

      yes your approach is more intuitive

    • @himanshusomani7
      @himanshusomani7 4 роки тому +2

      alll negative and zero also then first method also works

  • @abhishekshankar1136
    @abhishekshankar1136 4 роки тому +17

    This was asked in samsung and VMware interview too

  • @manas_singh
    @manas_singh 4 роки тому +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 4 роки тому +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

  • @mitulmunjal3379
    @mitulmunjal3379 4 роки тому +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.

  • @abhishekvishwakarma9045
    @abhishekvishwakarma9045 4 роки тому +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 😎🔥🔥

  • @dileepmenon1948
    @dileepmenon1948 4 роки тому

    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 4 роки тому

      yes, u r right about that!!

  • @prasannanivas4575
    @prasannanivas4575 4 роки тому +11

    The same code in python runs timeout in interviewbit

    • @utkarshagarwal8390
      @utkarshagarwal8390 4 роки тому

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

    • @PhoenixRisingFromAshes471
      @PhoenixRisingFromAshes471 4 роки тому +1

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

    • @PhoenixRisingFromAshes471
      @PhoenixRisingFromAshes471 4 роки тому

      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 3 роки тому

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

  • @nehamodak9660
    @nehamodak9660 4 роки тому +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.

  • @charan775
    @charan775 4 роки тому

    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

  • @ianku
    @ianku 2 роки тому

    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

  • @ganeshchowdhary3024
    @ganeshchowdhary3024 4 роки тому +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

    • @RachitJain
      @RachitJain  4 роки тому +4

      Not to 1, but probably n+1.

  • @imranwahid9871
    @imranwahid9871 3 роки тому +3

    Best explanation ever on this question. Thanks a lot.

  • @rituparnakhaund54
    @rituparnakhaund54 4 роки тому +5

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

    • @Mercer80
      @Mercer80 3 роки тому

      why while loop is used ???

  • @03_abhishekchoubey42
    @03_abhishekchoubey42 3 роки тому

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

  • @sammyshm
    @sammyshm 4 роки тому +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

  • @pallavikankure9698
    @pallavikankure9698 2 роки тому +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 2 роки тому +1

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

  • @fireluffy9156
    @fireluffy9156 4 роки тому +2

    What if the elements in given array are not distinct...like there are duplicates. For example the given array is : 1,3,3,6,7,8,9,9,10.

  • @DHWANIL19
    @DHWANIL19 4 роки тому

    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.

  • @yogeshwarsharma5041
    @yogeshwarsharma5041 4 роки тому +4

    Yes, Its good answer but What about if array has duplicate numbers.

    • @PhoenixRisingFromAshes471
      @PhoenixRisingFromAshes471 4 роки тому

      Exactly that what i was thinking...it will surely fail for duplicates.........to solve this segregate the positive and negative numbers (keep all the negatives to left of positives)and then apply the first algo on the positive part

    • @vineetwidhani8513
      @vineetwidhani8513 4 роки тому

      Before swapping we will check if the index already has a correct value or not, if not then we will swap or else we will continue.

  • @srijansinha7732
    @srijansinha7732 4 роки тому +2

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

    • @gollasaivenkatesh
      @gollasaivenkatesh 3 роки тому

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

  • @lasue7244
    @lasue7244 4 роки тому

    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 3 роки тому

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

  • @aakarshitrekhi8071
    @aakarshitrekhi8071 3 роки тому

    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

  • @_U_will_always_shine
    @_U_will_always_shine 3 роки тому +2

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

    • @ruralbaby1921
      @ruralbaby1921 3 роки тому

      it brought my confidence down to 0 :'( c

    • @_U_will_always_shine
      @_U_will_always_shine 3 роки тому

      @@ruralbaby1921 It's alright. We can always learn what we don't know.

  • @hemantjain8777
    @hemantjain8777 2 роки тому

    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.

  • @minnamreddyvenkataprasanth5959
    @minnamreddyvenkataprasanth5959 4 роки тому +2

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

  • @electra_
    @electra_ 2 роки тому

    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.

  • @awalktoinfinity665
    @awalktoinfinity665 4 роки тому +3

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

    • @dipeshpatil2771
      @dipeshpatil2771 4 роки тому

      The algorithm works for 0 also. Do check it yourself

  • @sagarkrishnabose8612
    @sagarkrishnabose8612 4 роки тому +1

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

  • @shvmsh20
    @shvmsh20 3 роки тому +1

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

  • @yashSharma-pe1dp
    @yashSharma-pe1dp 4 роки тому

    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.

  • @MANIDEEP08
    @MANIDEEP08 3 роки тому

    This is the simplest and beautiful solution.
    Thanks man!

  • @shreekrishna6144
    @shreekrishna6144 4 роки тому +1

    This is the bucketing technique.Thanks again man!

  • @utkarshgupta213
    @utkarshgupta213 4 роки тому

    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 2 роки тому

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

  • @shaikhubaidahmed3609
    @shaikhubaidahmed3609 4 роки тому

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

  • @vyshnavramesh9305
    @vyshnavramesh9305 3 роки тому +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.

  • @shubhamdebnath714
    @shubhamdebnath714 4 роки тому

    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 4 роки тому

      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 4 роки тому

      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

  • @parashararamesh4252
    @parashararamesh4252 4 роки тому

    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 4 роки тому

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

  • @mthaha2735
    @mthaha2735 3 роки тому

    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

  • @jatinsharma1915
    @jatinsharma1915 2 роки тому

    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.

  • @arihannnt
    @arihannnt 4 роки тому +6

    Amazing question ! 👌 And amazing solution ! 💯

  • @vinit472
    @vinit472 4 роки тому +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.

  • @suchismitagoswami5609
    @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.

  • @ganeshchowdhary3024
    @ganeshchowdhary3024 4 роки тому

    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

  • @vedavyas5953
    @vedavyas5953 4 роки тому

    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 4 роки тому

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

  • @adityashukla7562
    @adityashukla7562 4 роки тому +2

    What if we first sort the array then start traversing the array from start and then break the loop when a[ i ] != i and the value of i will be our required answer...
    Is this approach correct?
    And if not then why not?????

    • @amansingh9835
      @amansingh9835 4 роки тому +2

      Because sorting operation has a time complexity of O(n^2) in worst case and O(nlogn) in average case and we have to solve this problem in O(n). But the approach is correct if we ignore the time complexity.

    • @adityashukla7562
      @adityashukla7562 4 роки тому

      @@amansingh9835 we could use merge sort that has complexity of nlogn in worst case

    • @jatinsingh6744
      @jatinsingh6744 4 роки тому

      @@adityashukla7562 clearly in the video it is described that we need to do it in time complexity of O(n)

    • @adityashukla7562
      @adityashukla7562 4 роки тому

      @@jatinsingh6744 traversing the array will be of complexity O(n) and sorting will take O(nlogn) and eventually our final complexity will come out to be O(n).
      If I am wrong then please correct me.

    • @abhishekshankar1136
      @abhishekshankar1136 4 роки тому +1

      @@adityashukla7562 bro nlogn > n , so sorting itself will take O( nlogn ) time,

  • @animatoworldsharmapooja9996
    @animatoworldsharmapooja9996 4 роки тому +2

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

  • @jonty3551
    @jonty3551 4 роки тому +2

    What about doing an in-place sorting Algorithm like quick sort and then iterate over the array to find the result......would this work???

    • @amansingh9835
      @amansingh9835 4 роки тому +1

      Because sorting operation has a time complexity of O(n^2) in worst case and O(nlogn) in average case and we have to solve this problem in O(n). But the approach is correct if we ignore the time complexity.

    • @jonty3551
      @jonty3551 4 роки тому

      @@amansingh9835 oo.... didn't thought of that😅😬

  • @divyarajsinhrana2094
    @divyarajsinhrana2094 4 роки тому +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???

  • @kanhaiyasharma5834
    @kanhaiyasharma5834 4 роки тому +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 3 роки тому

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

  • @maheswaran6628
    @maheswaran6628 2 роки тому

    thank you for this simple and clear explanation brother

  • @ashishchoksi8501
    @ashishchoksi8501 4 роки тому +1

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

  • @nikhilneela
    @nikhilneela 4 роки тому

    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 ?

  • @miteshkumar3183
    @miteshkumar3183 4 роки тому

    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.

  • @akshatmangal474
    @akshatmangal474 4 роки тому +2

    for i in range(len(nums)+1):
    if i+1 in nums:
    continue
    else:
    return i+1
    break
    this code for the same question on Leetcode (as provided link in your description) got accepted then why to take this complex approach.

    • @gauravsonkusle3888
      @gauravsonkusle3888 4 роки тому

      can someone respond to this.. i have the same doubt... is it possible that there's a problem with leetcode?

    • @Yeager098
      @Yeager098 4 роки тому

      its a O(n^2) approach because of this statement " if i+1 in nums: " , "in" is O(n) operation as you are searching the whole array.

    • @gauravsonkusle3888
      @gauravsonkusle3888 4 роки тому

      @@Yeager098 yeah..thought the same... but it got accepted on leetcode...why? how can that happen? is there something wrong with leetcode?

    • @Yeager098
      @Yeager098 4 роки тому

      @@gauravsonkusle3888 it got accepted because the time constraints were not that tight and you are using python as it's slow so they make the time constraints a little weak as compared to other languages.

    • @gauravsonkusle3888
      @gauravsonkusle3888 4 роки тому

      @@Yeager098 okay... got ur point...thanks for replying bro

  • @MadForCs16
    @MadForCs16 3 роки тому

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

  • @rubydevi7405
    @rubydevi7405 3 роки тому +1

    What if the array contains duplicates ?

  • @developer-world9040
    @developer-world9040 4 роки тому

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

  • @abhi-5783
    @abhi-5783 4 роки тому

    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

  • @syamsreemanojreddy1027
    @syamsreemanojreddy1027 3 роки тому

    in the approach using swaps how did you handle duplicates?

  • @sohamsen7892
    @sohamsen7892 4 роки тому +1

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

  • @kaushikrishi01
    @kaushikrishi01 4 роки тому +11

    What software is used to make these sort of explaination videos and small animations ?

    • @akashp4863
      @akashp4863 4 роки тому

      Edits the video and gives voice to it

    • @BarkaDog
      @BarkaDog 4 роки тому

      @@akashp4863 bruh

  • @pqrabcd
    @pqrabcd 4 роки тому

    How would this work if the array has numbers greater than indices? eg. [300,400,700,100].

  • @prashnought
    @prashnought 4 роки тому

    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?

  • @sushanthgupta626
    @sushanthgupta626 4 роки тому +1

    what if numbers are repeating like [1 , 1] then the while loop will not stop.

    • @snlagr
      @snlagr 4 роки тому

      check the second condition in while loop of code implementation. if the current value is equal to the value we are going to swap with we break out.

  • @dhanubhardwaj6935
    @dhanubhardwaj6935 4 роки тому

    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.

  • @udaykumar555
    @udaykumar555 4 роки тому

    Hi Rachit,
    if we maintain smallest and second smallest number while we traverse through the array,then we can find out the missing smallest positive number right which basically lies in between these two numbers

    • @RachitJain
      @RachitJain  4 роки тому

      1,2,3,6
      How will your approach work in this?

  • @prabhatrai6292
    @prabhatrai6292 4 роки тому

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

  • @AdityaSingh-lf7oe
    @AdityaSingh-lf7oe 2 роки тому

    How about this approach o(n) time, o(1) space.. 1) Use quicksort algorithm to swap all -ve numbers to the beginning of the array 2) Now from position x to n, we have only +ve numbers 3) Now we can use the +/- technique to denote which positive integers are present in the array, by making that index -ve

  • @harshal101096
    @harshal101096 3 роки тому

    how come last solution is not having time complexity of (N2)???

  • @raghibanwar826
    @raghibanwar826 4 роки тому

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

  • @swatibahl7799
    @swatibahl7799 4 роки тому

    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.

  • @amitharitwal9378
    @amitharitwal9378 4 роки тому +1

    In the second approach we can use -INF

  • @crisag.2698
    @crisag.2698 4 роки тому

    Wouldn't the first approach you gave not work either if there existed negative elements in the array?

  • @satyampandey4600
    @satyampandey4600 4 роки тому

    But this code will not work if elements of array are similar, while loop will run indefinitely.

  • @Weninaminute
    @Weninaminute 3 роки тому

    How do I solve this question if the input array has repeated values e.g. [1, 1, 7, 3, 4]

  • @toxicdesire8811
    @toxicdesire8811 4 роки тому

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

  • @mathmyth9061
    @mathmyth9061 3 роки тому

    Rachit bhaiya what if an array containing duplicate values too?

  • @aakashsharma5901
    @aakashsharma5901 3 роки тому

    Too simple and great explanation bro

  • @RomanReigns-tg5qm
    @RomanReigns-tg5qm 4 роки тому +2

    We can do it using multiplication by -1 technique also. We just need to make all the numbers less than equal to 0, some invalid number like n+1 , or INT_MAX.

    • @RachitJain
      @RachitJain  4 роки тому +1

      Yeah that should work too! Good finding.

  • @manojjain3501
    @manojjain3501 3 роки тому

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

  • @akshatshah6413
    @akshatshah6413 3 роки тому

    Cant we run a loop and replace all negative numbers with n+1

  • @snehals2802
    @snehals2802 4 роки тому

    How about multiplying a number with a large prime number instead of -1 for array which can contain negative numbers..and then for checking which is missing number simply mod all the elements of array by that prime number.will this work?

  • @GGxEpsilon
    @GGxEpsilon 4 роки тому

    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?

  • @rohanseth1392
    @rohanseth1392 4 роки тому

    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

  • @sradhanjansaha185
    @sradhanjansaha185 2 роки тому

    a while loop within a for loop, wont it give a time complexity of O(N^2)??

  • @debdutsaha4316
    @debdutsaha4316 4 роки тому

    where you are doing in O(N)? You are iterating over all the elements and then swapping untill the number has the valid position so where is the timecomplexity O(N) follows? I didn't understand that.Please anyone has idea about this please let me know.

    • @RachitJain
      @RachitJain  4 роки тому

      I did mention in the video about this aspect. Probably watch again.

    • @debdutsaha4316
      @debdutsaha4316 4 роки тому

      bdw good work bro..you're my inspiration. Please make a video series in DP how to figure out that what problem goes under dp. If it is possible then make a series.

  • @CRTagadiya
    @CRTagadiya 4 роки тому

    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 .

  • @zkkzkk32312
    @zkkzkk32312 10 місяців тому

    This wouldn't work if the input has duplicate?

  • @abhayagarwal6871
    @abhayagarwal6871 4 роки тому

    What if there is zero in the array. How will you get the correct position then ?🤔

  • @sevarth9263
    @sevarth9263 4 роки тому

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

  • @bhavin_ch
    @bhavin_ch 4 роки тому

    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?

  • @partharanke5925
    @partharanke5925 2 роки тому

    do you think that they will ask questions like these in the first round
    of interview?