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

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

КОМЕНТАРІ • 33

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

    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.

  • @ramanujachanduri2253
    @ramanujachanduri2253 9 місяців тому

    Really loved your explanation among all the other UA-cam 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 😄

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

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

  • @shubhamsamdani3503
    @shubhamsamdani3503 4 роки тому +7

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

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

      Not stopped. But, will be adding selectively.

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

    Great explanation Sir.Thanks

  • @satyammishra-xb3hs
    @satyammishra-xb3hs 3 роки тому +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 Рік тому

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

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

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

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

    nice explanation

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

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

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

      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)

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

    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;
    }

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

    so good!

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

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

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

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

  • @fffooccc9801
    @fffooccc9801 9 місяців тому

    why can't we use mex here ?

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

    I think the problem really starts after 6 minutes

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

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

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

    Awesome

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

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

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

      I did the same way could you elaborate your approach?

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

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

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

      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

  • @ss-xh3hf
    @ss-xh3hf 4 роки тому

    why are we converting negavtives into 1 .

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

      Because we can't have negative index

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

    It's very annoying questions