58 58 votes The following function computes the maximum value contained in an integer array $P[ \ ]$ of size $n$ $(n>=1)$. int max (int *p,int n) { int a = 0, b=n-1; while (__________) { if (p[a]<= p[b]) {a = a+1;} else {b = b-1;} } return p[a]; } The missing loop condition is: $a\ \ != n$ $b\ \ != 0$ $b>(a+1)$ $b\ \ != a$ Programming in C gatecse-2016-set1 programming-in-c normal + – Sandeep Singh 18.2k views answer comment Share Follow Print See all 5 Comments 5 5 Comments reply Show 2 previous comments vishalshrm539 commented Aug 21, 2018 reply Follow flag If option C was b >= (a+1), then it would have worked! 20 20 replyShare araj52270 commented Sep 12, 2018 reply Follow flag @once_2019 why are thinking about factorial in this question 1st and 2nd option gave only for distraction 0 0 replyShare priyangsu commented Jan 23 reply Follow flag https://youtu.be/kkWGpD9OEuI?si=uQiMRYxi4sz8DGC- video link 1 1 replyShare Please log in or register to add a comment.
Best answer 90 90 votes Answer is (D). Hint : Given in the question itself that we start comparing the contents of an array from $a[0]$ and $a [n-1]$ (converging from both side) then condition must be till both meet at a point and that point will be $a=b$. Hence loop condition should be $a!=b$. Option C fails for $n=2, p = [1, 2].$ sukanyac answered Feb 12, 2016 • edited Jun 15, 2018 by Milicevic3306 sukanyac comment Share Follow See all 19 Comments 19 19 Comments reply Show 16 previous comments Amcodes commented Nov 27, 2020 reply Follow flag Yes it’s correct, in that case also loop terminates when b=a 🙂 0 0 replyShare Kiyoshi commented Sep 21, 2021 i edited by Kiyoshi Jun 25, 2022 reply Follow flag Above loop give correct answer when loop condition is :-b >= (a+1)b > abut it gives wrong result on :b > (a+1)b >= a 11 11 replyShare Eevee commented Nov 23, 2024 reply Follow flag @Kiyoshi During the return an additional condition should be checked if loop condition is $a+1 \le b$.if(p[a]> p[b]){return p[a]}; else return p[b]; If the condition is $a + 1 <= b,$ the loop can terminate when $a$ and $b$ are adjacent. In this case a and b may point to two different elements so, the final maximum value must be explicitly determined by comparing $p[a]$ and $p[b]$ after the loop. 0 0 replyShare Please log in or register to add a comment.
1 1 vote So option d is correct. ansurajjaishankar answered Jul 8 ansurajjaishankar comment Share Follow 0 reply Please log in or register to add a comment.
1 1 vote METHOD-1METHOD-2 Sudo_404_Div answered Jul 12 Sudo_404_Div comment Share Follow 0 reply Please log in or register to add a comment.