ProgrammingHardProduct company OA · GATE CS
What is printed?
What is printed?
int calls = 0;
int mx(int *a, int lo, int hi) {
calls++;
if (lo == hi) return a[lo];
int m = (lo + hi) / 2;
int l = mx(a, lo, m), r = mx(a, m + 1, hi);
return l > r ? l : r;
}
int main(void) {
int a[] = {4, 11, 7, 2, 9, 3};
int best = mx(a, 0, 5);
printf("%d %d", best, calls);
}- A. 11 10
- B. 11 11
- C. 9 11
- D. 11 6
Correct answer: B. 11 11
The maximum is 11. Divide and conquer on n elements makes a full binary recursion tree with n leaves and n − 1 internal nodes, so it makes 2n − 1 = 11 calls. '6' counts only the leaves.
More C Programming questions
- What is printed? c int a[] = {1, 2, 3}, p = a; int x = ++p++; int y = p++ 10; printf("%d %d %d %d", x, y, a[0], p);
- What is printed? c int x = 10; int p = &x, pp = &p; printf("%d", pp + p 2);
- What is printed? c const char s = " 42, 17"; printf("%zu %zu", strspn(s, " "), strcspn(s, ","));
- A header config.h is wrapped as #ifndef CONFIGH / #define CONFIGH / … / #endif. What problem does this prevent?
- With 4-byte int, after these lines run, which block can no longer be freed because no pointer to it remains? c int a = malloc(10…
- What is printed? c int pal(const char s, int i, int j) { if (i >= j) return 1; if (s[i] != s[j]) return 0; return pal(s, i + 1, j - 1); }…