cmi-entrance 2024 Q16

cmi-entrance · India · mscds Not Maths
16. In the following code, A is an array indexed from 0 whose elements are all positive integers, n is the number of elements in A , and x is a positive integer. The call abs( z ) returns the absolute value of integer z .
\begin{verbatim} function foo(A,n,x) { max = 0; for i from 0 to (n-1) { for j from (i+1) to (n-1) { diff = abs(A[i]-A[j]); if (diff >= x) and (diff >= max) { max = diff; } } } return(max); } \end{verbatim}
If $A = [ 10,8,10,4,10,7,1,2,2,9 ]$, what will $f _ { \circ \circ } ( A , 10,5 )$ return?
(a) 2
(b) 4
(c) 6
(d) 9
16. In the following code, A is an array indexed from 0 whose elements are all positive integers, n is the number of elements in A , and x is a positive integer. The call abs( z ) returns the absolute value of integer z .

\begin{verbatim}
function foo(A,n,x) {
    max = 0;
    for i from 0 to (n-1) {
        for j from (i+1) to (n-1) {
            diff = abs(A[i]-A[j]);
            if (diff >= x) and (diff >= max) {
                max = diff;
            }
        }
    }
    return(max);
}
\end{verbatim}

If $A = [ 10,8,10,4,10,7,1,2,2,9 ]$, what will $f _ { \circ \circ } ( A , 10,5 )$ return?\\
(a) 2\\
(b) 4\\
(c) 6\\
(d) 9\\