I want to create a recursion function that returns the minimum options to create a certain number using numbers 1, 5 and 7 (Fixed predetermined numbers). It is important that this is done only by recursion without loops at all.
For example:
if n = 10 then it is given to the scheme by 5 + 5 which is 2 numbers, so this is the minimum and this is what we will get (as opposed to 7 + 1 + 1 + 1 or 5 + 1 + 1 + 1 + 1 + 1 that is 4 or 6 Options that are longer).
If n = 6 then we get a result of 2 (because it is given as a sum of 1 + 5).
If n = 5 (or 7 or 1) then we get a result of 1 (because it is given by the number only).
class TEST {
static int countMin( int S[], int m, int n ,int min)
{
if (n == 0)
return 1;
if (n < 0)
return 0;
if (m <=0 && n >= 1)
return 0;
return Math.min(min,countMin( S, m - 1, n ,min-1) + countMin( S, m, n-S[m-1],min-1 ));
}
public static int f(int n) {
int arr[] = {1, 5, 7};
return countMin(arr, arr.length, n,n);
}
public static void main(String[] args)
{
int n = 10;
System.out.println("The number "+n+ " - " + f(n) + " minimum options to create");
int n2 = 7;
System.out.println("The number "+n2+ " - " + f(n2) + " minimum options to create");
int n3 = 6;
System.out.println("The number "+n3+ " - " + f(n3) + " minimum options to create");
}
}
I get for n = 10 and n = 5 for the correct result but not for n = 6 which should return 2.
*I've used this link: https://www.geeksforgeeks.org/coin-change-dp-7/