0

In how many ways, can you pay N dollar with 1 dollar, 2 dollar & 5 dollar denominations, in such a way that

  • the number of 1 dollar coins are always greater than that of 2 dollar coins and number of 2 dollar coins are always greater than that of 5 dollar coins.
  • Note: At least one coin should be given from each denomination.
  • Constraints 1 <= N <= 100

I tried something like this, but am not able to get how to check the constraints

    private static void numberOfWays(int number) {
    int one = 0, two = 0;
    int five = (number - 4) / 5;
    if (((number - 5 * five) % 2) == 0)
      {
    one = 2;
      }
    else
      {
    one = 1;
      }
    two = (number - 5 * five - one) / 2;
    System.out.println (one + two + five);
    System.out.println (five);
    System.out.println (two);
    System.out.println (one);}
Vaibhav Atray
  • 208
  • 4
  • 14

2 Answers2

1

The constraints are interesting.

"At least one coin should be given from each denomination" means that there are no solutions for N < 7.

"The number of 1 dollar coins are always greater than that of 2 dollar coins and number of 2 dollar coins are always greater than that of 5 dollar coins" means that numFives = 1, numTwos = 2, and numOnes = 3 to start, so there are no solutions for N < 12.

This satisfies both constraints.

Knowing that, how do you plan to attack it? Brute force?

It sounds like a variation on the knapsack problem. Maybe you can read about that.

I think simplex method and dynamic programming with constraints is your best bet.

duffymo
  • 305,152
  • 44
  • 369
  • 561
1

You could try something like this:

IntStream.rangeClosed(1, 100).forEach(target -> {

    final int max1 = (target + 1 - 1) / 1;
    final int max2 = (target + 2 - 1) / 2;
    final int max5 = (target + 5 - 1) / 5;

    IntStream        .rangeClosed(         1, max5).forEach(count5 -> {
        IntStream    .rangeClosed(count5 + 1, max2).forEach(count2 -> {
            IntStream.rangeClosed(count2 + 1, max1).forEach(count1 -> {

                final int sum = count5*5 + count2*2 + count1*1;

                if (sum == target) {
                    System.out.println("Target.: " + target + " -> " + count5 + "*5$ " + count2 + "*2$ " + count1 + "*1$");
                }
            });
        });
    });
    System.out.println();
});

...but, as others have pointed out, your constraints make a solution for some amounts impossible.

Here's a variation of this theme, giving a solution for all N...

IntStream.rangeClosed(1, 100).forEach(target -> {

    final int max1 = (target + 1 - 1) / 1;
    final int max2 = (target + 2 - 1) / 2;
    final int max5 = (target + 5 - 1) / 5;

    for         (int count1=         max1;              count1 > 0; count1--) {
        for     (int count2=Math.min(max2, count1 - 1); count2 > 0; count2--) {
            for (int count5=Math.min(max5, count2 - 1); count5 > 0; count5--) {

                final int sum = count5*5 + count2*2 + count1*1;

                if (sum == target) {
                    System.out.println("Target..............: " + target + " -> " + count5 + "*5$ " + count2 + "*2$ " + count1 + "*1$");
                    return; // Note.: solution found, exit from Consumer, NOT Method!
                }
            }
        }
    }
    /*
     * Fallback 1: at least one of each denomination...
     */
    for         (int count1=max1; count1 > 0; count1--) {
        for     (int count2=max2; count2 > 0; count2--) {
            for (int count5=max5; count5 > 0; count5--) {

                final int sum = count5*5 + count2*2 + count1*1;

                if (sum == target) {
                    System.out.println("Target (fallback 1).: " + target + " -> " + count5 + "*5$ " + count2 + "*2$ " + count1 + "*1$");
                    return; // Note.: solution found, exit from Consumer, NOT Method!
                }
            }
        }
    }
    /*
     * Fallback 2: "anything goes"...
     */
    for         (int count1=max1; count1 >= 0; count1--) {
        for     (int count2=max2; count2 >= 0; count2--) {
            for (int count5=max5; count5 >= 0; count5--) {

                final int sum = count5*5 + count2*2 + count1*1;

                if (sum == target) {
                    System.out.println("Target (fallback 2).: " + target + " -> " + count5 + "*5$ " + count2 + "*2$ " + count1 + "*1$");
                    return; // Note.: solution found, exit from Consumer, NOT Method!
                }
            }
        }
    }
    System.out.println("Target..............: " + target + " NO Solution possible!");
});
Dave The Dane
  • 650
  • 1
  • 7
  • 18