‹ Back to the paper
The following function getIt() is a part of some class. Assume x is a positive integer, f is the…
The following function getIt() is a part of some class. Assume x is a positive integer, f is the lower bound of arr[ ] and l is the upper bound of the arr[ ]. Answer the questions given below along with dry run/working.
public int getIt(int x,intarr[],int f,int l)
{
if(f>l)
return -1;
int m=(f+l)/2;
if(arr[m]<x)
return getIt(x,m+1,l);
else if(arr[m]>x)
return getIt(x,f,m-1);
else
return m;
} (a)[2.0]
What will the function getIt( ) return if arr[ ] = {10,20,30,40,50} and x=40?
(b)[1.0]
What is function getIt( ) performing apart from recursion?
Answer
Answer (a)
Official answer keyThe function getIt() performs a Binary Search on the sorted array. For $arr[\ ]=\{10,20,30,40,50\}$, $x=40$, $f=0$, $l=4$ (dry run, verified by running the corrected code):
Call 1: getIt(40,arr,0,4) -> m=(0+4)/2=2, arr[2]=30<40 -> call getIt(40,arr,3,4)
Call 2: getIt(40,arr,3,4) -> m=(3+4)/2=3, arr[3]=40==40 -> returns m=3
The function returns 3.
Answer (b)
Official answer keyApart from recursion, the function getIt() is performing a Binary Search on the sorted array arr[ ] to locate the position (index) of the value x, returning its index if found, or -1 if x is not present in the array.
From ISC 2025 Specimen Computer Science Paper 1, question 2(iii).