0% found this document useful (0 votes)
22 views

Binary Search

Binary Search - Notes

Uploaded by

Apoorva Ranjan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Binary Search

Binary Search - Notes

Uploaded by

Apoorva Ranjan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

BINARY SEARCH

1. Find unique element in a sorted array where all elements appear twice
except one element –
• Every duplicate element will appear together so unique won’t be equal
to element next to it – T: O(n)
• Or, take XOR of all elements – T: O(n)
• Or, use binary search – T: O(log n)
• Duplicate elements before unique element will occur at even-odd
indexes and duplicate elements after unique element will occur at
odd-even indexes
• So, if mid is even, check if a[mid] == a[mid + 1], if it is start = mid + 1
else end = mid
• If mid is odd, check if a[mid] == a[mid – 1], if it is start = mid + 1 else
end = mid – 1
• Repeat above steps until (start <= end) and when start == end, we’ve
found the unique element i.e. a[start]
2. Find first and last position of an element in a sorted array –
• Use linear search. FirstIndex = -1, LastIndex = -1. If target found, check
if FirstIndex == -1, if it is FirstIndex = i else LastIndex = i – T: O(n)
• Or, use binary search – T: O(log n)
• Two functions: findFirstIndex (array, target) and findLastIndex (array,
target)
• findFirstIndex: Compare at mid, if target found, set FirstIndex = mid
and move to left. If target < a[mid], move to left else move to right
• findLastIndex: Compare at mid, if target found, set LastIndex = mid and
move to right. If target < a[mid], move to left else move to right
3. Find frequency of an element in a sorted array –
• Find first and last index using above technique
• Frequency = LastIndex – FirstIndex + 1
• Don’t forget to consider the case when element is not present in the
array. In that case, the above formula doesn’t apply, you simply have
to return -1.
4. Compute square root of an integer –
• Check for every number from x = 1 until x * x <= n. Finally, ans = (x - 1)
– T: O(sqrt(n))
• Or, use binary search – T: O(log n)
• Take start = 1, end = n or n / 2 (optimization)
• Check if mid * mid == n, ans = mid, if mid * mid < n, start = mid + 1 else
end = mid – 1. Finally, ans = end
5. Search in rotated sorted array –
• Use linear search – T: O(n)
• Or, use binary search – T: (log n)
• In a rotated sorted array, at least one half of the array will always be
sorted.
• Compare target with mid, if found, ans = mid
• Else check if left half is sorted or not i.e. a[start] <= a[mid]. If it is, now
check if target lies in this half or not i.e. target >= a[start] && target <=
a[mid], if yes, move left else move right
• If left half is not sorted, then right half will be sorted. Now check if
target lies in right half or not i.e. target >= a[mid] && target <= a[end],
if yes, move right else move left
• Repeat the above process until start <= end and outside the loop
return -1
6. Leftmost column with at least a one –
• The given matrix is a binary matrix with all its rows sorted from left to
right
• Only two methods are provided – i) you can call get(i, j) to get the value
in that cell and ii) you can call dimensions() to get the dimensions of
the matrix
• Use binary search, call binary search on each row and finally return the
minimum index among them – T: O(n*log n)
• Or, apply modifies binary search – T: O(m + n)
• Find row and col values using dimensions()
• Start from the top right corner i.e. i = 0, j = col – 1 and let ans = -1
• If you find a 0, move i forward, else ans = j and move j backwards
• Repeat the above process until (i < row && j >= 0)
7. First bad version –
• We’ve ‘n’ versions of a product which contains few good versions and
few bad versions. We need to find the first bad version.
• isBadVersion(version) method returns a Boolean, true if the version is
bad and false if the version is good
• Use linear search in [1, n). Return i when the version is found bad, if
no version is found bad, return n – T: O(n)
• Or, use binary search with start = 1, end = (n – 1) – T: O(log n)
• If found bad at mid, end = mid else start = mid + 1
• Finally, ans = start
8. Valid Perfect Square –
• Use linear search from [2, n / 2] (return true for 0 and 1). If i * i == n,
return true and if i * i > n, return false. Return false outside the loop
as well. – T: o(n)
• Or, use binary search with start = 2, end = (n / 2) – T: O(log n)
• If mid * mid == n, return true. If mid * mid > n, move left else move
right. Return false outside the loop
9. Floor and ceil of an element in a sorted array –
• Floor: Largest element smaller than or equal to x
• Traverse the whole array and whenever a larger element is found,
return the previous index. Return -1 outside the loop – T: O(n)
• Or, use binary search – T: O(log n)
• Compare at mid, if found, return mid
• If x < a[mid], move left else ans = mid and move right
• Ceil: Smallest element greater than or equal to x
• Traverse the whole array and whenever x > a[i] && x <= a[i + 1], return
i + 1 – T: O(n)
• Or, use binary search – T: O(log n)
• Compare at mid, if found, return mid
• If x > a[mid], move right else ans = mid and move left
10. Find the peak element in sorted array –
• Peak element: An element whose both neighbors are smaller than that
• For first and last elements, only one neighbor is considered
• If all the elements are equal, every element is the peak element
• Traverse the whole array, compare each element with its previous and
next. Whenever the condition – a[i] >= a[i – 1] && a[i] >= a[i + 1] is
satisfied, return i – T: O(n)
• Or, use binary search – T: O(log n)
• Check if mid is peak or not, if it is, return mid
• If its not, check if next element is greater than the mid, if yes, move
right
• If previous element is greater than the mid, move left

You might also like