Over here i am getting output twice

public static void main(String[] args) {
int[] arr = {1,2,36,39,44,49,50,90,99};
int item = 44;
int low =0;
int hi =arr.length - 1;
while(low<=hi)
{
int mid = (hi + low)/2;
if(item>arr[mid]) {
low=mid+1;
}
else if(item<arr[mid]) {
hi = mid-1;
}
else{
System.out.println(mid);
}low ++;
}
}
}

@rishabh.chhabra10,
The values of low and hi are:
0 and 8 for the first answer.
1 and 8 for the second answer.

BUT, mid = (low + hi)/2, hence (0+8)/2 = 4 and (1+8)/2 = 4. Hence the value being printed twice :sweat_smile: Because the value of mid is not changing.

@rishabh.chhabra10,
You can refer to the code here:

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.