Slice Down Problem in Java, Hacker Rank Interview Question
Slice Down problem in Java (Hacker Rank Interview Questions)
Input: array of integer value
Output: print each iteration by subtracting the smallest elements till array becomes empty
For example:
For input array: 4, 3, 5, 8, 12
At each iteration, find minimum number and subtract it from element and print non zero elements
1 2 5 9 (Subtracted 3 from each elements)
1 4 8 (Subtracted 1 from each elements)
3 7 (Subtracted 1 from each elements)
4 (Subtracted 3 from each elements)
For the above problem, naive approach is to find the minimum in each iteration, subtracting it and copy it in new memory.
There are 2 place where you can improve on memory as well as the time complexity (less iteration).
1. Identify minimum value while subtracting the minimum value
2. Do not copy element to another array, just simple store the last index value and decrease it in recursive call.
Below is the screen shots of my project directory and the code for the above solution:
You can also clone the repository from GIT using the link below:
Read full article from Slice Down Problem in Java, Hacker Rank Interview Question
No comments:
Post a Comment