GATE 2016 CS (CS2) – Question 47
What does this program print? max returns the larger argument.
int f(int *p, int n) {
if (n <= 1) return 0;
return max(f(p+1,n-1),p[0]-p[1]);
}
int main() {
int a[] = {3,5,2,6,4};
printf("%d",f(a,5));
}Practise this question in The GATE Grind →
Show answer and explanation
Correct answer: 3.0 to 3.0
Explanation
The recursive calls compare all adjacent differences against base value zero. The differences are −2, 3, −4 and 2. Their maximum together with zero is 3. Therefore the program prints 3; it does not compute the maximum difference between arbitrary nonadjacent elements.