ProgrammingVery hardProduct company OA
What is printed?
What is printed?
int c = 0;
int f(int n) { c++; return n <= 1 ? 1 : f(n / 2) + f(n - n / 2); }
int main(void) {
int r = f(7);
printf("%d %d", r, c);
}- A. 7 13
- B. 8 15
- C. 7 7
- D. 7 14
Correct answer: A. 7 13
Each call splits n into two parts that add up to n, down to pieces of size 1, so the result is n itself: 7. The recursion tree has 7 leaves and, being a full binary tree, 6 internal nodes: 13 calls.
More C Programming questions
- Which of these loops run their body exactly for i = 4, 3, 2, 1, 0 (once each) with an unsigned i? c for (unsigned i = 5; i-- > 0; ) { /…
- What is printed? c int x = -1; unsigned y = 1; printf("%d", (x < 0 ? x : y) < 0);
- What is printed? c int a[] = {1, 2, 3, 4, 5, 6}; int s = 0; for (int p = a; p < a + 6; p += 2) s += p p[1]; printf("%d", s);
- What is printed? c int cat(int n) { if (n <= 1) return 1; int s = 0; for (int i = 0; i < n; i++) s += cat(i) cat(n - 1 - i); return s; }…
- What is printed? c char comp[31] = {0}; int cnt = 0; for (int i = 2; i <= 30; i++) { if (comp[i]) continue; cnt++; for (int j = i i; j <=…
- What is printed? c int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } int main(void) { int l = 1; for (int i = 1; i <= 10; i++) l = l…