All problems

Problem 92

First and Last Position of Element

Medium · 20 pts · Array, Binary Search

Problem

You are given an array of N integers sorted in non-decreasing order, and a target T. Print the first and the last index (zero-based) at which T appears. If T does not appear at all, print -1 -1. Run binary search twice: once for the leftmost position whose value is at least T, and once for the leftmost position whose value is greater than T. If the first position holds T, the answer is that position and the second position minus 1. This takes O(log N) time.

Input

First line: integers N and T. Second line: N space-separated integers in non-decreasing order.

Output

Print two zero-based indices separated by a space, or -1 -1.

Constraints

1 <= N <= 100000; -1000000000 <= A[i], T <= 1000000000

Example input

6 8
5 7 7 8 8 10

Example output

3 4

Explanation

In 5 7 7 8 8 10, the target 8 appears at indices 3 and 4. The first occurrence is 3 and the last is 4, so the output is 3 4.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Search Insert Position Next: Search in Rotated Sorted Array