# Open Closed range in a sorted array

By | November 13, 2015
Share the joy
•
•
•
•
•
•

1. Find leftOpen
For a sorted array 1, 2, 3, 4, 4, 4, 5, 5, 6. And given 4. Find the left-most position, where 4>=arr[pos]. The answer of position is 3

2. Find leftClose
For a sorted array 1, 2, 3, 4, 4, 4, 5, 5, 6. And given 4. Find the left-most position, where 4>arr[pos]. The answer of position is 6

Similarly, there are problem to find right open and right close. This is a very useful question. And I finally wrote it for practice.

My general process for left open and left close.

1.Move to right as far as possible. When mid is less than n, then we should find the right side. right = mid + 1;

2. Or it means mid is at below somewhere. Then we check if arr[mid-1] is eligible for the right answer.

3. Else it means both mid-1 and mid are at the right part. We should move to left.

Because we don’t want arr[mid-1] out of index boundary. At the beginning, check if pos=0 is a valid position. Similar ways are applicable to check right close and right open scenario.

Check my code on github: link