Given n
versions we are required to find the first bad version since every following version after the first bad version is bad. We know for a fact that n
th version is bad, we need to find out when did it start going bad.
This problem is identical to finding the pivot in a rotated sorted array. Searching in a linear manner can sure find the first bad version, but it won’t be very efficient. If , searching is going to be time-consuming. Instead, lets just do a binary search -
Code:
Java
Big O Analysis
-
Runtime
The runtime complexity here is
O(log N)
- Binary Search!. -
Memory
The memory usage is
O(1)
since we no linear/non-linear data structure used.
— A