TCS | Codevita | Reduce to zero
Anonymous User
434
Mar 23, 2022
Mar 28, 2022

Java code for the problem--
Rahul is given a positive number N and he needs to reduce it to 0 (zero) by following operations-
Take one of the divisors of N - which is different from N itself - and subtract it from N.
Perform the above operation till the original number is reduced to 0.
The task is to find the minimum number of steps Rahul needs to perform such that N is reduced to 0.

Example-
Input : 5
Output : 4
Explanation-
Divisors of 5 are 1, 5 but you cannot subtract 5 so then subtract 1 from 5.
The reduced number is 5 - 1 = 4.
Divisors of 4 are 1,2 and 4. Now subtract 2 and number becomes 2.
Then number becomes 2 -1 = 1.
You can subtract 1 by 1 and number becomes 0.
So, for N=5, min number of steps are 4.

Comments (3)