forked from sachuverma/DataStructures-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFirst and last occurrences of X.cpp
74 lines (62 loc) · 1.27 KB
/
First and last occurrences of X.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
First and last occurrences of X
===============================
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.
Note: If the number x is not found in the array just print '-1'.
Input:
The first line consists of an integer T i.e number of test cases. The first line of each test case contains two integers n and x. The second line contains n spaced integers.
Output:
Print index of the first and last occurrences of the number x with a space in between.
Constraints:
1<=T<=100
1<=n,a[i]<=1000
Example:
Input:
2
9 5
1 3 5 5 5 5 67 123 125
9 7
1 3 5 5 5 5 7 123 125
Output:
2 5
6 6
*/
#include <bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin >> t;
while (t--)
{
int n, x;
cin >> n >> x;
vector<int> arr(n);
for (auto &i : arr)
cin >> i;
int first = -1, last = -1;
for (int i = 0; i < n; ++i)
{
if (arr[i] == x)
{
first = i;
break;
}
}
if (first == -1)
{
cout << -1 << endl;
continue;
}
for (int i = n - 1; i >= 0; --i)
{
if (arr[i] == x)
{
last = i;
break;
}
}
cout << first << " " << last << endl;
}
return 0;
}