TIME AND SPACE COMPLEXITY / ONLINE JUDGE › ONLINE JUDGE
The statement asks you to print the 0-based index of the first occurrence of x, not the value x itself. For the array [4, 2, 7, 2, 1] and x = 2, the value 2 appears at indices 1 and 3. The first occurrence is at index 1, so the required result is 1. Because indexing starts at 0, the first array element has index 0, making the answer 1 rather than 2.
The sample output is meaningful only when you connect it to every word in the required result. Printing 2 would print the target value, not its position. Printing 3 would print the position of a later match. Printing 1 is correct because it is the position of the first matching value under 0-based indexing.
For the concrete array [4, 2, 7, 2, 1] and x = 2, what does the problem print?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
Read the first line as two tokens in order: the token 5 becomes n, and the token 2 becomes x. The second line contains exactly n array values, so the five tokens become a[0] = 4, a[1] = 2, a[2] = 7, a[3] = 2, and a[4] = 1.
5 2
4 2 7 2 1Line breaks explain the input format to a human reader, but they do not change the token order. A typical C++ or Java input reader consumes whitespace, including spaces and newlines, in the same sequence. The program must still read n and x first, then read exactly n array values, because that order is the contract.
The constraint 1 <= n <= 100000 says the array is never empty and may be much larger than the concrete five-element sample. A solution must therefore treat n as the number of values to read, rather than assuming that five values always exist or reserving logic for only the shown sample.
The bounds -1000000000 <= a[i], x <= 1000000000 apply to every array value and to x, not just to the concrete values 4 and 2. Those bounds fit within a standard 32-bit signed integer range, so a C++ int or Java int can represent each input value. The constraint gives a storage fact, while n gives the number of values the input can contain.
Type the variable binding produced by the concrete input, including n, x, and all five array values.
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
Start at index 0 and compare the array value with x. At index 0, a[0] is 4, so the search continues. At index 1, a[1] is 2, which equals x, so the search stops immediately. The later match at index 3 is irrelevant because the first occurrence has already been found.
| INDEX | ARRAY VALUE | COMPARISON WITH X | RESULT |
|---|---|---|---|
| 0 | 4 | 4 != 2 | continue |
| 1 | 2 | 2 == 2 | stop and print 1 |
The concrete input produces the exact output 1. Before writing code, turn the statement into four checks: read n and x in that order, read exactly n array values, stop at the first value equal to x, and print its 0-based index. If no value equals x, the required output is -1. The sample values cannot replace any of these rules, because a different valid input may have a different length, target, or matching position.