• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests

GATE | GATE CS 1996 | Question 66

Consider the following program that attempts to locate an element x in a sorted array a[ ] using binary search. Assume N>1. The program is erroneous. Under what conditions does the program fail?

var i,j,k: integer;  x: integer;
    a: array; [1....N] of integer;
begin	i:= 1; j:= N;
repeat	
    k:(i+j) div 2;
    if a[k] < x then i:= k 
    else j:= k 
until (a[k] = x) or (i >= j);
    
if (a[k] = x) then
    writeln (\'x is in the array\')
else
    writeln (\'x is not in the array\')
end;

(A)

x is the last element of the array a[]

(B)

x is greater than all elements of the array a[]

(C)

Both of the Above

(D)

x is less than the last element of the array a[]

Answer

Please comment below if you find anything wrong in the above post
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!

Last Updated :
Share your thoughts in the comments