Find Index
Practice this coding question on Coding Gurukul.
Problem statement
A warehouse log lists package category codes in arrival order. An auditor wants to locate the first and last recorded arrivals of one chosen category. Your task Find the first and last zero-based indices of x in the array. Print -1 -1 if x never appears. Important details The log is not necessarily sorted, and matching arrivals need not be adjacent. If there is exactly one match, print its index twice. Return positions rather than matching values. Scan the entire log so that a later occurrence is not missed. Index zero refers to the first entry. Worked example For target 4 and log 4, 2, 4, 7, 4, the matching indices are 0, 2, and 4. Print 0 4.
Input format
The first line contains n x. The second line contains n integers.
Output format
Print the first and last indices separated by a space.
Constraints
1 <= n <= 100000; -1000000000 <= a[i] <= 1000000000. -1000000000 <= x <= 1000000000.
Sample input
5 4 4 2 4 7 4
Sample output
0 4
Sign in to run or submit a solution in C++, Java or Python.