First and Last Position of Element

MediumBinary Search
Next
Binary SearchArray

In a sorted array that may contain duplicates, find the first and last index of a target value. If the target is absent, report -1 -1. Aim for O(log n).

Input

Line 1: n and the target. Line 2: n integers in non-decreasing order.

Output

Print the first and last index separated by a space, or -1 -1 if the target is not present.

Constraints

1 <= n <= 10^5 -10^9 <= each value, target <= 10^9

Example 1
Input
6 8
5 7 7 8 8 10
Output
3 4

8 occupies indices 3 and 4.

Example 2
Input
6 6
5 7 7 8 8 10
Output
-1 -1

6 is not in the array.

Hints

First and Last Position of Element - Medium Binary Search Problem | Skill Shiksha