Missing Numbers (Hackerrank)

Sometimes the easiest of the questions can become cumbersome and actually put our knowledge to the test. The following question had me writing code for 30 minutes, only to be rejected by the compiler telling me that I was missing something!

Question- 






















Approach - 
  • Make a new array with last element = 0  
  • Input all values of Bigger array
  • Delete similar values found in Smaller array
  • Print all the values which are higher than 0
Now what took me so long?
I made nested for loops to check every value of A if it lies in B, if it doesn't, I would append this value to an empty array, sort it and print it.

But when i did that, It gave me repeating values and the output was in the form of m+n where m and n are sizes of the array respectively. Hence, I had to do some brainstorming and research which led me to a realization that i can simply add all elements of bigger array brr[m] in a new array C, then delete the same values from C taking elements of arr[n] into consideration! 

Then simply output the numbers in ascending order :) 

Code-
So what did i learn?
When in doubt, don't waste time coding and falling into a pit, simply google concepts and remember them for future.


Writing a blog about it can help ;) 

Popular posts from this blog

Finding the Subarrays (Hackerearth)

Find Missing Number (Amazon SDE)