Loading...
Loading...
Loading...
A canteen hands out numbered tokens, and some numbers are given out more than once by mistake. Given the list of tokens in order and a token number x, print the first position of x, the last position of x and how many times x appears. Positions start at 1.
-1 -1 0.Example 1
Input
7 4 9 2 9 5 9 1 9
Output
2 6 3
9 is at positions 2, 4 and 6.
Example 2
Input
3 1 2 3 8
Output
-1 -1 0
8 is not in the list.
Try the problem first. Open one hint at a time.
Keep first = -1, last = -1 and count = 0 before the loop.
On a match, set first only if it is still -1; always set last and add 1 to count.
Print index + 1, because positions start at 1.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor