ProgrammingVery hardCompetitive coding (CP)

How many partitions of 6 into distinct parts are there? The recursion below counts them. What is printed?

How many partitions of 6 into distinct parts are there? The recursion below counts them. What is printed?
#include <stdio.h>
int dp(int n, int maxp) {
    if (n == 0) return 1;
    int t = 0;
    for (int p = (maxp < n ? maxp : n); p >= 1; p--)
        t += dp(n - p, p - 1);
    return t;
}
int main(void) {
    printf("%d", dp(6, 6));
}
  1. A. 4
  2. B. 3
  3. C. 5
  4. D. 6

Correct answer: A. 4

The distinct-part partitions of 6 are 6, 5+1, 4+2 and 3+2+1, which makes 4. The parameter maxp forces the parts to be strictly decreasing, so no part repeats.

More C Programming questions

Exams that ask this topic