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));
}- A. 4
- B. 3
- C. 5
- 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
- How many ways are there to tile a 1 by 4 strip with 1 by 1 and 1 by 2 tiles? The recursion computes it. What is printed? c #include…
- What is printed? The program computes the parity of the set bits of 0b1101. c #include <stdio.h> int parity(unsigned x) { return x == 0 ?…
- What is the position of the lowest set bit of 0x0300 (counting bit 0 as position 0)? Compute with this program. c #include <stdio.h> int…
- What is printed? The program inverts the Gray code 6 by XOR-folding its bits. c #include <stdio.h> int main(void) { unsigned g = 6, n = 0…
- What is printed? c #include <stdio.h> int main(void) { unsigned char c = 200; if (c 2 > 300) puts("big"); else puts("small"); }
- What is the difference between the largest and the smallest element of {4, -2, 9, 0}, found in a single pass with pointers? Compute it…