Find first and last occurrence of x
Submissions: 18187   Accuracy:

34.6%

  Difficulty: Basic   Marks: 1
Associated Course(s):   Sudo Placement 2019

Given a sorted array with possibly duplicate elements. The task is to find indexes of first and last occurrences of an element X in the given array.

Input:
The first line of input contains an integer T denoting the no of test cases. Then T test cases follow. Each test case contains an integer N denoting the size of the array. Then in the next line are N space separated values of the array. The last line of each test case contains an integer X.

Output:
For each test case in a new line print two integers separated by space denoting the first and last occurrence of the element X. If the element is not present in the array print -1.

Constraints:
1 <= T <= 100
1 <= N <= 1000
1 <= A[], X <= 1018

Example:
Input:

2
9
1 3 5 5 5 5 67 123 125
5
9
1 3 5 5 5 5 7 123 125
7

Output:
2 5
6 6

Explanation:
Testcase 1:
Index of first occurrence of 5 is 2 and index of last occurrence of 5 is 5.

 

** For More Input/Output Examples Use 'Expected Output' option **

Author: Shubham Joshi 1


If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.



Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.


to report an issue on this page.