![]() |
What is the binary search algorithm?
How to solve a question using Binary Search:
And the task is to find the maximum value of x such that f(x) is less than or equal to the target value. The interval in which we will search the target value for the given
is from 0 to target value.
is a monotonically increasing function.
Below is the implementation of the above example: C++
Java
Python3
C#
Javascript
Output: 9 The above binary search algorithm requires at most O(log N) comparisons to find the maximum value less than or equal to the target value. And the value of the function f(x) = x2 doesn’t need to be evaluated many times.
|
Reffered: https://www.geeksforgeeks.org
Searching |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 11 |