 New update is available. Click here to update.

# First and Last Position of an Element In Sorted Array

Last Updated: 23 Feb, 2023
Easy 0/40
Avg time to solve 15 mins
Success Rate 85 % Share 393 upvotes

## Problem Statement

#### Note :

``````1. If ‘K’ is not present in the array, then the first and the last occurrence will be -1.
2. ARR may contain duplicate elements.
``````

#### For example, if ARR = [0, 1, 1, 5] and K = 1, then the first and last occurrence of 1 will be 1(0 - indexed) and 2.

Detailed explanation ( Input/output format, Notes, Images ) ##### Constraints:
``````1 <= T <= 100
1 <= N <= 5000
0 <= K <= 10^5
0 <= ARR[i] <=10^5

Time Limit : 1 second
``````
##### Sample Input 1:
``````2
6 3
0 5 5 6 6 6
8 2
0 0 1 1 2 2 2 2
``````
##### Sample output 1:
``````-1 -1
4 7
``````
##### Explanation of Sample output 1:
``````For the first test case, 3 is not present in the array. Hence the first and last occurrence of 3 is -1 and -1.

For the second test case, the first occurrence of 2 in at index 4 and last occurrence is at index 7.
``````
##### Sample Input 2:
``````2
4 0
0 0 0 0
1 2
2
``````
##### Sample output 2:
``````0 3
0 0
``````  Auto Console