ProgrammingHardProduct company OA

What is printed?

What is printed?
int ways(int amt, int k) {
    static const int c[] = {1, 2, 5};
    if (amt == 0) return 1;
    if (amt < 0 || k == 0) return 0;
    return ways(amt - c[k - 1], k) + ways(amt, k - 1);
}
int main(void) { printf("%d", ways(10, 3)); }
  1. A. 8
  2. B. 12
  3. C. 10
  4. D. 88

Correct answer: C. 10

Each step either uses the largest allowed coin again or drops it for good, so combinations are counted once regardless of order. With 0, 1 or 2 fives the remainders 10, 5 and 0 can be made from 1s and 2s in 6, 3 and 1 ways: 10 in total. Counting orders would give far more.

More C Programming questions

Exams that ask this topic