First Missing Positive | Leetcode 41(Hard-tag) Solution in Hindi

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

КОМЕНТАРІ • 43

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

    After watching lot of youtube videos and reading articles finally my search ended here.
    thanku sir..

  • @sahilnegi2789
    @sahilnegi2789 3 роки тому +8

    how to think this type of appraoch

  • @tanusharma6875
    @tanusharma6875 6 місяців тому

    very well explained!

  • @dookiedoo2
    @dookiedoo2 3 роки тому +10

    Samay Raina teaching Java
    Nice

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

      🤣🤣🤣🤣🤣

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

    if there is any pepcoding video available then i will definitely watch that .

  • @ajaymourya3527
    @ajaymourya3527 2 роки тому +5

    approach is good but work on how to teach to make all understand

  • @info-tech6041
    @info-tech6041 Рік тому

    very good explanation

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

    thank you sireesh sir

  • @Satishkumar-rx7oy
    @Satishkumar-rx7oy 3 роки тому +1

    great sir. thanking for making the explanation so simple

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

    Great Sir, You explained it very well...

  • @arslanali-vi5nj
    @arslanali-vi5nj 4 місяці тому

    so nice

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

    Mast approach tha bhai. Thanks a lot.

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

    but why it is working??? I'm not clear about it..

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

      because first you are making all the elements in range , then you mark negative the element where it should have been present ,all the negative elements in the end will represent the found numbers and whichever is not marked from the left will be our answer.

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

    Awesome approach sir.. Maza agya

  • @chief9482
    @chief9482 Рік тому

    Why this approach worked?? I mean if I give this solution to someone else then how can I prove this solution will always work?

    • @SidharthjainSingla
      @SidharthjainSingla 11 місяців тому +1

      Think of its as , first you are searching for 1 if it is present make the flag equal to true and if any time you see a number which is less than or equal to zero or the number is greater than the size of array then it is of no use for us, because we don't want any negative numbers and if the array length is n and if there are all numbers from 1 to n in this , it means n+1 is the smallest missing positive number and that is why if any number in the array is greater than size of the array then it is of no use for us, then why we are mapping, because we know now all the numbers in the array now in the range of 1 to n, we know hashing concepts to search for a element faster i.e, in O(1), why we are making nums[index - 1] as negative ? why not nums[index] ? The answer is because we want only smallest positive number , smallest positive number starts from 1, if you encounter 1, how to use the indexes for faster access of elements? we know that zero is not present in the array now, so we can 0 index for that if 1 is present or not and same 1 index we can use if 2 is present or not, i.e, we are saying if n is present, then index n-1 must be negative, that is why we are marking nums[index-1] as negative and also you can think if the length of array is n and if n is present in the array , then how can you identify if n is present because n index is not present in array of size n, that is why we are using i-1 indexing for checking if i element is present, then now you will finally find the missing number by again traversing the updated array, if the index is negative it means index+1 is present in the array and if any element you encounter which is greater than 0 , then it means the index in which this element is present, it means index+1 is not present in the array, because if it was present, then nums[index-1] must had been negative, if you searched all the array , you can't find a single positive element in the array , it means n+1 is not present in the array.

  • @josephstark5810
    @josephstark5810 Рік тому

    we can mark out of range 0 and while map if arr[i]==0 continue;
    else arr[arr[i]-1]=-ve

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

    Bhai we can't use extra space Boolean array

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

      We havent used extra space. Extra space means, with the increase of input size, your extra space should increase. In our case, we have a constant variable, i.e even if n=1000, there will be one boolean and if n==1, there will be one boolean.

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

      bro wo variable h naki array

  • @MrChetan54
    @MrChetan54 2 роки тому +1

    Good explaination

  • @whitedracarys7613
    @whitedracarys7613 2 роки тому +2

    approach clear nhi hai
    aur why to pata hi nhi chala

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

    Is there any solution without modifying the input in time O(n) and space O(1)?

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

      Yes. We will do in-place swapping in the array such that every index has value same as the index+1 and ignore negative and greater than n(array.length)values. Then the first index which doesn't have same value as the index+1 will give us the answer .Answer will be index+1.

    • @Phantomm2019
      @Phantomm2019 2 роки тому +1

      class Solution {
      public: int firstMissingPositive(vector A) {
      int n = A.size();
      for(int i = 0; i < n; ++ i) {
      while(A[i] >= 1 && A[i]

    • @heya2k198
      @heya2k198 Рік тому

      ​@@Phantomm2019 But wouldn't that actually mean modifying the input array?

    • @Phantomm2019
      @Phantomm2019 Рік тому

      ​@@heya2k198 yess it will modify the array....I didn't notice the requirement of not modifying array😅

  • @doomhead9109
    @doomhead9109 2 роки тому +1

    Not working for the test case [3,4,-1,1]

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

      array length is 4 and numbers in range 4 so all good. and in first loop nothing to mark as 1 since all in range and boolean for 1 found is also true. Then iterate. first element is 3 so 3-1 = 2 but at index 2 value is already negative so no update required, then value is 4 so 4-1=3 make 3rd index value as -1, then -1 take absolute of it which is 1 so 1-1=0 make 0th index value as negative then again 1 so 1-1 is 0 and 0th index value is already negative so no change. Then in next iteration we got index 1 which is having +ve value so answer will be 1+1 = 2 which is missing

  • @aniketshukla8085
    @aniketshukla8085 Рік тому

    Awesome

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

    wow kya approach hai

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

    I have dry ran .This will not work for 2
    0 1

  • @ashwanirawat1385
    @ashwanirawat1385 2 роки тому +1

    2nd method is wrong because it can contain duplicate or more than one value

  • @agyaani8060
    @agyaani8060 2 роки тому +1

    Niceee❤

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

    Sir code ka font toda increase kr do