Advertisement
vaibhav1906

First Bad Version

Nov 24th, 2021
1,064
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.47 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     int firstBadVersion(int n) {
  4.        
  5.         int l=1, h = n;
  6.         //Binary search to find first true for the condition isBadVersion(m)==true
  7.         while(l<h){
  8.             int m = l+ (h-l)/2;
  9.            
  10.             if(isBadVersion(m)==true){
  11.                 h = m;
  12.             }
  13.            
  14.             else{
  15.                 l = m+1;
  16.             }
  17.            
  18.         }
  19.        
  20.         return l;
  21.        
  22.        
  23.     }
  24. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement