jayati

First Bad Version

Mar 19th, 2023
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.64 KB | None | 0 0
  1. // The API isBadVersion is defined for you.
  2. // bool isBadVersion(int version);
  3.  
  4. class Solution {
  5. public:
  6.     int firstBadVersion(int n) {
  7.          long long int start = 0 ;
  8.         long long int end = n ;
  9.         long long int minBadVersion ;
  10.          
  11.         while (start <= end)
  12.         {
  13.             long long int mid = (start + end) / 2 ;
  14.  
  15.             if (isBadVersion(mid) == 1)
  16.             {
  17.                 minBadVersion = mid ;
  18.                 end = mid - 1 ;
  19.             }
  20.             else
  21.             {
  22.                 start = mid + 1 ;
  23.             }
  24.         }
  25.         return minBadVersion ;
  26.        
  27.     }
  28. };
Advertisement
Add Comment
Please, Sign In to add comment