Given a function ‘int f(unsigned int x)’ which takes a non-negative integer ‘x’ as input and returns an integer as output. The function is monotonically increasing with respect to value of x, i.e., the value of f(x+1) is greater than f(x) for every input x. Find the value ‘n’ where f() becomes positive for the first time. Since f() is monotonically increasing, values of f(n+1), f(n+2),… must be positive and values of f(n-2), f(n-3), .. must be negative.
Find n in O(logn) time, you may assume that f(x) can be evaluated in O(1) time for any input x.
A simple solution is to start from i equals to 0 and one by one calculate value of f(i) for 1, 2, 3, 4 .. etc until we find a positive f(i). This works, but takes O(n) time.
Can we apply Binary Search to find n in O(Logn) time? We can’t directly apply Binary Search as we don’t have an upper limit or high index. The idea is to do repeated doubling until we find a positive value, i.e., check values of f() for following values until f(i) becomes positive.
f(0) f(1) f(2) f(4) f(8) f(16) f(32) .... .... f(high) Let 'high' be the value of i when f() becomes positive for first time.
Can we apply Binary Search to find n after finding ‘high’? We can apply Binary Search now, we can use ‘high/2’ as low and ‘high’ as high indexes in binary search. The result n must lie between ‘high/2’ and ‘high’.
Number of steps for finding ‘high’ is O(Logn). So we can find ‘high’ in O(Logn) time. What about time taken by Binary Search between high/2 and high? The value of ‘high’ must be less than 2*n. The number of elements between high/2 and high must be O(n). Therefore, time complexity of Binary Search is O(Logn) and overall time complexity is 2*O(Logn) which is O(Logn).
The value n where f() becomes positive first is 12
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Meta Binary Search | One-Sided Binary Search
- Binary Search for Rational Numbers without using floating point arithmetic
- Best First Search (Informed Search)
- Find minimum value to assign all array elements so that array product becomes greater
- Interpolation search vs Binary search
- Why is Binary Search preferred over Ternary Search?
- Linear Search vs Binary Search
- Find the maximum element in an array which is first increasing and then decreasing
- Remove minimum elements from the array such that 2*min becomes more than max
- Minimum in an array which is first decreasing then increasing
- Find the transition point in a binary array
- Sublist Search (Search a linked list in another list)
- Repeatedly search an element by doubling it after every successful search
- Check if a Binary Tree contains node values in strictly increasing and decreasing order at even and odd levels
- Find square root of number upto given precision using binary search
- Find H-Index for sorted citations using Binary Search
- Find the smallest positive number missing from an unsorted array | Set 2
- Find minimum positive integer x such that a(x^2) + b(x) + c >= k
- Best meeting point in 2D binary array
- Find a Fixed Point (Value equal to index) in a given array