First Missing Positive | LeetCode 41 | C++, Java, Python | BITWISE operation

Sdílet
Vložit
  • čas přidán 29. 09. 2020
  • LeetCode Solutions: • LeetCode Solutions | L...
    Github Link: github.com/KnowledgeCenterYou...
    *** Best Books For Data Structures & Algorithms for Interviews:*********
    1. Cracking the Coding Interview: amzn.to/2WeO3eO
    2. Cracking the Coding Interview Paperback: amzn.to/3aSSe3Q
    3. Coding Interview Questions - Narasimha Karumanchi: amzn.to/3cYqjkV
    4. Data Structures and Algorithms Made Easy - N. Karumanchi: amzn.to/2U8FrDt
    5. Data Structures & Algorithms made Easy in Java - N. Karumanchi: amzn.to/2U0qZgY
    6. Introduction to Algorithms - CLR - Cormen, Leiserson, Rivest: amzn.to/2Wdp8rZ
    *****************************************************************************
    September LeetCoding Challenge | Problem 30 | First Missing Positive | 30 September,
    Facebook Coding Interview question,
    google coding interview question,
    leetcode,
    First Missing Positive,
    First Missing Positive c++,
    First Missing Positive Java,
    First Missing Positive python,
    First Missing Positive solution,
    41. First Missing Positive,
    #CodingInterview #LeetCode #Google #Amazon

Komentáře • 33

  • @husler7424
    @husler7424 Před rokem +3

    I'm damn sure no better solution is available than this. Thanks a ton, sir ji! I was about to give up but found your video. Keep continue this series.

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

    Hi, Did you stoped making newer videos on Leetcode problems?

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

    Great explanation Sir.Thanks

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

    Nice. I finally get it. Your visuals are perfect.

  • @ramanujachanduri2253
    @ramanujachanduri2253 Před 4 měsíci

    Really loved your explanation among all the other CZcams channels present. I was only halfway through this video and it gave me enough confidence and clarity to solve this question by my own. Keep working on the good stuff 😄

  • @satyammishra-xb3hs
    @satyammishra-xb3hs Před 2 lety +3

    Sir , Excellent video , but sir I did not understand why do we have to do x=abs(arr[i ]) ??
    As we already put 1 for the negative values In the above loop ? So why we are again checking for positive??

    • @Music-tp8gg
      @Music-tp8gg Před rokem

      In between while traversing we are making the numbers -ve by multiplying with -1. So we take abs

  • @satyakiranmayibhamidimarri3160

    so good!

  • @avinashmaurya7819
    @avinashmaurya7819 Před 3 lety

    Sir ,I write the same code as you, it ran but my program didn't submit

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

    @Knowledge Center, It will be great if you can add leetcode problem "Word Break"

    • @ankoor
      @ankoor Před 3 lety

      Word Break can be solved using recursion + memoization:
      memo = {}
      wordDict = set(wordDict)
      def solve(s, wordDict):
      if s in wordDict:
      return True
      if s in memo:
      return memo[s]
      n = len(s)
      for i in range(1, n+1):
      if (s[:i] in wordDict) and solve(s[i:], wordDict):
      memo[s] = True
      return True
      memo[s] = False
      return False
      solve(s, wordDict)

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

    Awesome

  • @giit85
    @giit85 Před 2 lety

    But it will not work if there has already negative number or 0 in array

  • @shavisharma6581
    @shavisharma6581 Před 3 lety

    Please do make video on Leetcode Challenge September 29 - Word Break.

  • @beaglesnlove580
    @beaglesnlove580 Před rokem

    How the heck can you figure this out without seeing the dang solution for O(1) time. Crazy

  • @fffooccc9801
    @fffooccc9801 Před 4 měsíci

    why can't we use mex here ?

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

    nice explanation

  • @joydeeprony89
    @joydeeprony89 Před 2 lety

    there is an issue in your logic
    updated your logic, just checking the input array has 1 or not, in that case return 1 as an output
    public int FirstMissingPositive(int[] nums)
    {
    int smallestPositive = int.MaxValue;
    int length = nums.Length;
    // Step 1 -
    // loop entire array and mark the cells as 1 wherever cell value is N
    for (int i = 0; i < length; i++)
    {
    if(nums[i] > 0)
    {
    smallestPositive = Math.Min(smallestPositive, nums[i]);
    }
    if (nums[i] length)
    {
    nums[i] = 1;
    }
    }
    if (smallestPositive != 1)
    {
    return 1;
    }
    // Step 2 -
    // loop entire array and for each cell value -1 position, update to negetive to mark that value is present in range of 1-N
    for (int i = 0; i < length; i++)
    {
    int val = Math.Abs(nums[i]);
    if(nums[val - 1] > 0)
    nums[val - 1] *= -1;
    }
    // Step 3 -
    // loop entire array, any value as positive , that position + 1 is the missing element, if not found then N+1 is the missing element(eg- (1,2,3,4,5)=>O/p-6)
    for (int i = 0; i < length; i++)
    {
    if (nums[i] > 0) return i + 1;
    }
    return length + 1;
    }

  • @joydeeprony89
    @joydeeprony89 Před 2 lety

    not working for input - 7,8,9, 11, 12

    • @letslive3782
      @letslive3782 Před 2 lety

      It will work as he wrote a condition to check if 1 is present or not..if 1 is not present then 1 is already returned

  • @jax40001
    @jax40001 Před 2 lety

    I think the problem really starts after 6 minutes

  • @akashyadav3211
    @akashyadav3211 Před rokem

    4 for loops 😐...I did it using one while loop and one for loop.....

  • @ss-xh3hf
    @ss-xh3hf Před 3 lety

    why are we converting negavtives into 1 .

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

      Because we can't have negative index

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

    It's very annoying questions