Code Collab Critique
You're a senior developer at UptoSkills Arena, reviewing code submissions for a coding challenge. Each submission is a sequence of integers, representing line numbers where a particular type of syntax error occurred. You want to quickly identify, for each error, the most recent previous error of the same type. If no such previous error exists, you want to mark it with -1.
Given a sequence of error codes, find for each error code, the index of the nearest preceding occurrence of the same error code. If none exists, output -1.
Input
The first line contains an integer N (1 <= N <= 200000), the number of errors. The second line contains N space-separated integers $a_1, a_2, ..., a_N$ (-10^9 <= $a_i$ <= 10^9), the error codes.
Output
Print N space-separated integers, where the i-th integer is the index (1-based) of the nearest preceding occurrence of error code $a_i$. If no such occurrence exists, print -1.
Constraints
1 <= N <= 200000 -10^9 <= $a_i$ <= 10^9 Time Limit: 2000 ms Memory Limit: 256 MB
Example 1
7 10 20 10 30 20 10 40
-1 -1 1 -1 2 3 -1
For each error code, we find the nearest previous occurrence. For example, the third '10' (at index 2) has its nearest previous '10' at index 0. The second '20' (at index 4) has its nearest previous '20' at index 1.
Example 2
5 1 1 1 1 1
-1 1 2 3 4
All error codes are the same. Each error (except the first) has a preceding error of the same type.
Hints
# Read the input from stdin and print the answer.
import sys
def main():
data = sys.stdin.read().split()
# your code here
main()
7 10 20 10 30 20 10 40
5 1 1 1 1 1