68 68 votes Consider the following program: int f (int * p, int n) { if (n <= 1) return 0; else 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)); } Note: $\max (x, y)$ returns the maximum of $x$ and $y$. The value printed by this program is ________. Programming in C gatecse-2016-set2 programming-in-c normal numerical-answers recursion + – Akash Kanase 22.3k views answer comment Share Follow Print See all 4 Comments 4 4 Comments reply krishn.jh commented Dec 3, 2018 reply Follow flag Try this in http://pythontutor.com ------------------------------------------------------- int f (int * p, int n) { if (n <= 1) return 0; else return f(p+1, n-1)>(p[0] - p[1])?f (p+1, n-1):(p[0] - p[1]); } int main () { int a[] = {3, 5, 2, 6, 4}; printf(" %d", f(a, 5)); } 0 0 replyShare prkshprasad1 commented Jul 22, 2021 reply Follow flag @arjun sir, I think there is some typo. It will be printf() in main() instead of print f. As f is also a function is its bit confusing 0 0 replyShare parag parab commented Jan 2, 2024 reply Follow flag https://www.youtube.com/watch?v=h1ds9EuyWmU 2 2 replyShare chetan-naik commented Oct 8, 2024 reply Follow flag For an intutive understanding of the code, focus on the return statement of the function f. The function returns the maximum value between two expressions: the result of the recursive call and the difference between the first two elements of the array.As the recursion unfolds, each call evaluates the difference between adjacent elements and compares it with the recursively computed maximum difference. This process continues until all pairs of consecutive elements have been examined. Ultimately, the function computes the maximum difference among all the adjacent pairs, i.e., $\text{max}(a[i] - a[i+1])$, across the entire array. 0 0 replyShare Please log in or register to add a comment.
Best answer 92 92 votes $f(a,5)$ $p$ , $n=5.$ $\begin{array}{|l|l|l|l|l|} \hline 3 & 5 & 2 & 6 & 4 \\\hline \end{array}$ $\max(f(p+1,5-1),3-5) = \max(f(p+1,4),-2)$ $p$ , $n=4. \begin{array}{|l|l|l|l|} \hline 5 & 2 & 6 & 4 \\\hline \end{array}$ $\max(\max(f(p+1,4-1),5-2),-2)=\max(\max(f(p+1,3),3),-2)$ $p$ , $n=3. \begin{array}{|l|l|l|} \hline 2 & 6 & 4 \\\hline \end{array}$ $\max(\max(\max(f(p+1,3-1),2-6),3),-2) =\max(\max(\max(f(p+1,2),-4),3),-2)$ $p$ , $n=2.\begin{array}{|l|l|} \hline 6 & 4 \\\hline \end{array}$ $\max(\max(\max(\max(f(p+1),1),2),-4),3),-2)$ $n=1$, return $0$ $\max(\max(\max(\max(0,2),-4),3),-2)$ $=\max(\max(\max(2,-4),3),-2)$ $=\max(\max(2,3),-2)$ $=\max(3,-2)$ $=3$ Praveen Saini answered Feb 13, 2016 • edited Apr 14, 2019 by akash.dinkar12 Praveen Saini comment Share Follow See all 9 Comments 9 9 Comments reply Show 6 previous comments mrinmoyh commented Apr 3, 2019 reply Follow flag Can anyone tell me what this function do actually?? 0 0 replyShare adarsh_1997 commented Jul 22, 2019 reply Follow flag @MRINMOY_HALDER find the maximum difference if two subsequent array elements are subtracted . 1 1 replyShare Ishu_Thakur commented Sep 18, 2019 reply Follow flag post increment bro 0 0 replyShare Please log in or register to add a comment.
45 45 votes Ans is 3 Rajesh Pradhan answered Aug 6, 2016 Rajesh Pradhan comment Share Follow See all 10 Comments 10 10 Comments reply Show 7 previous comments asifalam commented Nov 24, 2019 reply Follow flag Can you please explain the reason behind the increment of Index in p[0] and p[1]. 0 0 replyShare Ekta07_GATE commented Nov 25, 2019 reply Follow flag @asifalam Firstly p+1 does not mean incrementing of p, instead p+1 is passed as parameter to p of function. But p[0]=3 p[1]=5 1 1 replyShare KHUSHI_SUDHAKAR_VAIS commented Aug 5, 2025 reply Follow flag Okk when p+1 passed as parameter it act as new pointer again just like we are again setting the new pointer from a+1? 0 0 replyShare Please log in or register to add a comment.
3 3 votes It can easily be solved by observing the code. Here in the code they are trying to figure out max(array[i]-array[i-1]) so answer will be max(a[0]-a[1],a[1]-a[2],a[2]-a[3],a[3]-a[4]) = max(3-5,5-2,2-6,6-4) = max(-2,3,-4,2) = 3 Madhurima Sen answered Mar 21, 2021 Madhurima Sen comment Share Follow 0 reply Please log in or register to add a comment.
3 3 votes Answer : 3 Sharadamani_K_N answered Nov 16, 2025 • edited Dec 2, 2025 by Sharadamani_K_N Sharadamani_K_N comment Share Follow See all 2 Comments 2 2 Comments reply mayanksinghchandel commented Nov 26, 2025 reply Follow flag how 6-4=-2 in last 0 0 replyShare Sharadamani_K_N commented Nov 26, 2025 reply Follow flag @mayanksinghchandel Thanks for pointing it out. I’ve corrected it now. 1 1 replyShare Please log in or register to add a comment.
1 1 vote solution is given below:- Shivam Patidar answered Jul 15, 2018 Shivam Patidar comment Share Follow 0 reply Please log in or register to add a comment.
1 1 vote Using recursion tree method shashankrustagi answered Dec 23, 2020 shashankrustagi comment Share Follow 0 reply Please log in or register to add a comment.