Open In App

Understanding Opposite Polarity Concept in Binary Search

Last Updated : 20 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Opposite Polarity is a concept in Binary Search which works on the search space with the condition that the before and after regions of the search space will be reversed when the Binary Search Algorithm is applied to it. In this post, we will understand how the Opposite polarity concept in Binary Search works with the help of a problem.

What is the Opposite polarity concept in Binary Search?

While using the Binary Search algorithm to solve any problem, we initially take two pointers- low and high, which denote the starting and end of the search space respectively. So, the Opposite polarity concept of Binary search states that, if low is initially pointing to the non-solution region and high is pointing to the solution region, then after ending of Binary Search algorithm, low will point to the solution region, and high will point to the non-solution region, or vice-versa.

Opposite-polarity-concept-in-Binary-Searchdrawio-(1)

Opposite polarity concept in Binary Search

Understanding Opposite polarity concept in Binary Search with the help of a Problem

Let’s recall the problem Koko Eating Bananas to understand it in a better way:

In this problem, we have to find the number of Bananas from the entire answer space such that, all the given bananas got eaten within H hours.

Considered an example of the above problem:

n = 4, piles = [3, 6, 7, 11], H = 8

Initial Configuration before applying Binary Search Algorithm

  • We can say that the answer to this problem will lie somewhere between in [1, 109].
  • Since there is no sense in adding a number in the answer space, which is greater than the maximum in the given array (11 in the above case), because at any of the rates: (>=11banana/hour). The hour will be always 4, which is useless here. If the answer space beyond 11 gives the same value as 11 and is also useless, then it is better to omit it.
  • So, it will be optimal to take answer region as [1, 11]. Which means Initially, low is initially as 1 and high is 11.
  • The answer to this test case is 4 (For understanding opposite polarity concept, answer is well known to us initially), which means at rate (<4 banana/hour) we can’t finish all bananas under 8 hours, while at rate (>=4 banana/hour) all bananas can be eaten within 8 hours.

Solution and Non-Solution Region before applying Binary Search

So these two points divides the entire search space into two regions:

  • Solution region: For this question, the solution region is from [4, 11], because at this rate, we can eat all bananas within 8 hours. Since, high is pointing to 11, therefore, it means high is pointing to solution region initially. (Note: The actual answer always lies in between the solution region. For example all the rates from (4-11)/hour, we can eat bananas within 8 hours, Since required answer is minimum rate. Therefore, answer is 4)
  • Non-Solution region: Low is initially pointing to 1, which is in the non-solution region [1, 3] because at any of rate between this region we can’t end up with all bananas. Therefore, Low is pointing to non-solution region initially.

Using Binary Search Algorithm

1. First Iteration:

BS1

  • For this iteration Mid : (1+11)/2 = 6.
  • At rate 6 banana/ hour. The hour will take to eat all bananas will be = (3/6)+(6/6)+(7/6)+(11/6) = 1+1+2+2 = 6.
  • Which is less than or equal to 8. So, this can be possible answer, but there can be more minimum rate than 6, therefore move to left region from mid. (Note: Ciel division took place in calculating hours).

2. Second Iteration:

BS2

  • Mid = (1+5)/2 = 3, The hour will take to eat all bananas will be = (3/3)+(6/3)+(7/3)+(11/3) = 1+2+3+4 = 10.
  • So, this can’t be possible answer, because hours are greater than 8, therefore move to right region from mid.

3. Third Iteration:

BS3

  • Mid = (4 + 5)/2 = 4. At this rate required hours will be = (3/4)+(6/4)+(7/4)+(11/4) = 1+2+2+3 = 8.
  • Which is less than or equal to 8. So 4 can be possible answer, But there can be more minimum rate per hour.
  • Therefore, shift high to left region of mid.

4. Fourth Iteration (Last Iteration):

BS4

  • As high>low, so binary Search ends here.

Solution and Non-Solution Region after applying Binary Search

We can see that initially low was in non solution region and high was in solution region. But after ending of Binary Search, high is in non solution region and low is in solution region.

Screenshot-from-2023-11-20-17-51-26

Solution and Non-Solution Region after applying Binary Search

This is the opposite polarity concept in Binary Search.

        



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads