#C3552. Find Occurrences of an Element in a List
Find Occurrences of an Element in a List
Find Occurrences of an Element in a List
Given a list of integers and an integer \(X\), your task is to determine the indices of the first and last occurrence of \(X\) in the list. The list is 0-indexed. If \(X\) does not appear in the list, output -1 for both indices.
For example, if the list is [1, 2, 2, 3, 4, 2, 5]
and \(X = 2\), the answer is 1 5
because the first occurrence of 2 is at index 1 and the last is at index 5. If \(X\) is not present in the list, output -1 -1
.
Input Format: The first line contains an integer \(n\), the number of elements in the list. The second line contains \(n\) space-separated integers. The third line contains the integer \(X\).
Output Format: Print two space-separated integers denoting the first and last indices of \(X\) in the list.
inputFormat
The input is read from standard input (stdin) and has the following format:
- The first line contains an integer \(n\) (the number of elements in the list).
- The second line contains \(n\) space-separated integers.
- The third line contains the integer \(X\) whose occurrences are to be found.
outputFormat
Output to standard output (stdout) two space-separated integers: the index of the first and the index of the last occurrence of \(X\) in the list. If \(X\) is not found, output -1 -1
.
7
1 2 2 3 4 2 5
2
1 5
</p>