Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

binary search

 
vijay umar
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
could any one help me out with the explanation of this question?

Given a properly prepared String array containing five elements, which range of results could a
proper invocation of Arrays.binarySearch() produce?
A. 0 through 4
B. 0 through 5
C. -1 through 4
D. -1 through 5
E. -5 through 4
F. -5 through 5
G. -6 through 4
H. -6 through 5

the answer is G.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vijay umar wrote:could any one help me out with the explanation of this question?

Given a properly prepared String array containing five elements, which range of results could a
proper invocation of Arrays.binarySearch() produce?
A. 0 through 4
B. 0 through 5
C. -1 through 4
D. -1 through 5
E. -5 through 4
F. -5 through 5
G. -6 through 4
H. -6 through 5

the answer is G.

If the element is found: Return something from 0 to 4 (I hope this one is clear.)
If the element is not found, the index returned will be the index where the item should be inserted (substracting 1) so that the order of the array is not disrupted. It can be inserted anywhere from index 0 to index 4 (-1 to -5.) Additionally, it would be possible that this item is larger than any other element in the string, which means it would be inserted at index 5 (then, binarySearch() would return -6.)
 
vijay umar
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Ruben!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic