Baseline Arrays · Easy O(n) · O(1)
An onboard diagnostics unit receives an unsorted batch of error codes and must confirm whether a specific fault code was reported. Because the batch arrives in arbitrary order there is nothing to exploit, so the lookup has to walk the readings until the code shows up. Report where the first matching reading sits, or -1 when the code never appears.
Input: An integer n, an array arr of n integers, and an integer k to look for.
Output: The 0-indexed position of the first occurrence of k, or -1 if k is absent.
1 <= n <= 10^5-10^9 <= arr[i] <= 10^9-10^9 <= k <= 10^9Input: {"n":6,"arr":[9,7,2,16,4,18],"k":16}
Output: 3
The readings are unsorted, so the scan advances until 16 appears at index 3.
Input: {"n":5,"arr":[1,2,3,4,5],"k":5}
Output: 4
The match is the final reading, so the scan has to reach the end of the batch.