First Bad Version Solutions in C++Number 278Difficulty EasyAcceptance 35.8%Link LeetCodeOther languages JavaSolutionsC++ solution by haoel/leetcode// Source : https://leetcode.com/problems/first-bad-version/// Author : Hao Chen// Date : 2015-10-19 // Forward declaration of isBadVersion API.bool isBadVersion(int version); class Solution {public: //Binary search int firstBadVersion(int n) { int low=1, high=n; while(low <= high) { int mid = low + (high - low)/2; if (isBadVersion(mid) && !isBadVersion(mid-1)){ return mid; } if (isBadVersion(mid)) { high = mid - 1; }else{ low = mid + 1; } } return -1; }};// Source : https://leetcode.com/problems/first-bad-version/ // Author : Hao Chen // Date : 2015-10-19 // Forward declaration of isBadVersion API. bool isBadVersion(int version); class Solution { public: //Binary search int firstBadVersion(int n) { int low=1, high=n; while(low <= high) { int mid = low + (high - low)/2; if (isBadVersion(mid) && !isBadVersion(mid-1)){ return mid; } if (isBadVersion(mid)) { high = mid - 1; }else{ low = mid + 1; } } return -1; } };