HackerRank - Missing Numbers | Full solution with examples and visuals

Поділитися
Вставка
  • Опубліковано 7 лип 2024
  • Missing Numbers is a programming challenge on HackerRank. You are given an array, and an artist is trying to transport those numbers to a different array. In the process, he misses some of those numbers. These are termed as missing numbers. We need to find these numbers in a sorted manner. Watch the video to understand the problem in a simplified manner. I then work along with you to solve it first using a Brute Force approach, and then an efficient approach. All along with visuals and explanations.
    00:00 - Intro
    01:04 - Problem Statement and Test Case
    03:11 - Brute Force Solution
    05:49 - Efficient Solution
    09:54 - Dry-run of code
    12:10 - Final thoughts
    📚 Links I talk about in the video:
    Actual problem on HackerRank: www.hackerrank.com/challenges...
    Code on Github: github.com/nikoo28/java-solut...
    Test cases on GitHub: github.com/nikoo28/java-solut...
    📘 A text based explanation is available at: studyalgorithms.com/array/hac...
    🔗 To see more videos like this, you can show your support on: www.buymeacoffee.com/studyalg...
    💻 Get Social 💻
    Follow on Facebook at: / studyalgos
    Follow on Twitter at: / studyalgorithms
    Follow on Tumblr at: / studyalgos
    Subscribe to RSS feeds: studyalgorithms.com/feed/
    #hackerrank #programming #tutorial

КОМЕНТАРІ • 30

  • @sikderabdurrahmanjony8741
    @sikderabdurrahmanjony8741 3 роки тому +5

    NOBODY EXPLAINED IN SUCH DETAILS LIKE YOU.GO ON.

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

      Thank you so much for your support.

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

    Great explanation brother

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

    THANK YOU BROTHER.

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

    Amazing, amazing, amazing...............

  • @unemployedcse3514
    @unemployedcse3514 Місяць тому

    awesome ❤

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

    Awesome 😊😎

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

    I just finished your strings playlist in which you had similar problem. In that, you used the pointer approach.
    I used similar strategy:
    function missingNums(a1, a2) {
    let i = 0//pointer for a1
    let result = []
    for (let j = 0; j < a2.length; j++) {
    if (a2[j] === a1[i]) {
    i++
    } else {
    result.push(a2[j])
    }
    }
    //sort in ascending order
    return result.sort((a, b) => a - b)
    }
    Which approach would be considered better?

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

      What is the time complexity of your approach?

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

    Hi Nikhil,
    Great Explanation.
    Can I ask you a doubt? I tried to work below code ,passed test cases except test case 1.
    Any suggestion would be appreciated
    def missingNumbers(arr, brr):
    brr_copy = brr.copy() # Create a copy of brr to avoid modifying the original list

    for i in arr:
    if i in brr_copy:
    brr_copy.remove(i)

    return sorted(brr_copy)

    • @nikoo28
      @nikoo28  8 місяців тому

      have you tried to debug with some test cases?

  • @Robert-ob8bu
    @Robert-ob8bu 2 роки тому +1

    1 test case is failing by use of map.......i also did while loop to managee multiple frequencies ....even then its failing 1 test case

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

      Can you give me an example of the test case. The solution I submitted on hackerrank passes all cases.

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

    I'm not a java guy but wonder time complexity of TreeMap to maintan the items in sorted order.

    • @nikoo28
      @nikoo28  11 місяців тому

      A TreeMap almost works in constant time.

  • @AjayMishra-ix3kj
    @AjayMishra-ix3kj 3 роки тому

    Wow

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

    can i use for this solution stream api in java?

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

      Yes, you should be able to

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

    ❤❤❤❤❤❤❤❤❤❤

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

    in case of repeted freequency that is if 1 is more than 1 then the code is not right.

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

      The code is correct. We subtract frequency by "1" for every repeated number. Can you show me a test case for which the code fails?

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

      @@nikoo28 i mean in case where 2 times 1 is there in IntegeerFreqmap after removing numbers from arr then in final loop how times 1 will be inserted. as result[i++]=IntegeerFreqmap.getKey() value in . We are not checking frequency to put in resultSet.

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

      Let us go over it step by step. Suppose you have two 1’s
      In the first loop you create the map and the frequency would be stored as “1 -> 2”
      In the next loop if you find one 1 in the other array you subtract a frequency so the map changes to “1 -> 1”
      In the final loop you look at the remaining elements in the frequency map. You find a frequency and hence the code works.

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

      @@nikoo28 got it I was thinking some else thing only read the question again thankyou so much

    • @vilakshan.s
      @vilakshan.s 2 місяці тому

      Still have similar doubt. What if in second array both the two 1s got missed from array 1 then in the MAP you will have like 1->2 then for such frequencies you will have to add the 1 entry 2 times in the final output

  • @AjayMishra-ix3kj
    @AjayMishra-ix3kj 3 роки тому

    Is there another way to solve it???

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

      You can try to solve it using a frequency array where each index stores the count of each element in first array

    • @AjayMishra-ix3kj
      @AjayMishra-ix3kj 3 роки тому

      @@nikoo28 okk