Advertisement
nikunjsoni

702

May 17th, 2021
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.65 KB | None | 0 0
  1. /**
  2.  * // This is the ArrayReader's API interface.
  3.  * // You should not implement it, or speculate about its implementation
  4.  * class ArrayReader {
  5.  *   public:
  6.  *     int get(int index);
  7.  * };
  8.  */
  9.  
  10. class Solution {
  11. public:
  12.     int search(const ArrayReader& reader, int target) {
  13.         int left = 0, right = 10000;
  14.        
  15.         while(left<=right){
  16.             int mid = (left+right)/2;
  17.             int val = reader.get(mid);
  18.             if(val == target)
  19.                 return mid;
  20.             else if(val > target)
  21.                 right = mid-1;
  22.             else
  23.                 left = mid+1;
  24.         }
  25.         return -1;
  26.     }
  27. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement