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 element)
1 4 8       (Subtracted 1 from each element)
3 7          (Subtracted 1 from each element)
4             (Subtracted 3 from each element)

For the above problem, a naive approach is to find the minimum in each iteration, subtracting it and copy it in new memory.

There are 2 places 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 simply store the last index value and decrease it in a 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:



Code is as below:

The output of the above code:

Again you can find the above code on git repo:

Leave a Reply

Your email address will not be published. Required fields are marked *