Binary Search
Binary search typically appears when the array is sorted or its left and right halves both possess the same properties
Last updated
Binary search typically appears when the array is sorted or its left and right halves both possess the same properties
Last updated
Is array sorted?
If not, can you sort it and still preserve information?
Can the array be separated into two distinct parts where the first half satisfies a condition while the other does not?
Move towards the direction of larger/smaller values relative to the mid-point chosen.
This does not work with arrays that can have duplicates (naively picking a direction to move in could be disastrous)
This can only produce local peaks
The arrays always exhibit the following properties:
nums[0] > nums[-1]
There are essentially 2 sorted arrays, the boundary can be found by comparing nums[i]
against nums[0]
If nums[i] > nums[0]
, i
is in the first half, otherwise, it is in the second half
Use nums[0]
as a marker for determining where i
is in the array
Try using the values of the arrays as a range or treating using the pure index like (0, 0)
to (n - 1, n - 1)
has the last index as (n * n) - 1
These problems are harder to notice but a common property that can be found is when the matrix is sorted in some order (row)
Always from [0, n)
Problems commonly test to see if you can identify these properties (such as or )
Problems can include or modifying binary search to search the matrix in a consistent manner such as