Loading...
Loading...
Loading...
A class has roll numbers 1 to n. The attendance sheet has n - 1 different roll numbers, in any order: exactly one student is absent. Find the missing roll number.
Example
Input
6 3 1 6 2 5
Output
4
1 + 2 + … + 6 = 21, and the present numbers add up to 17.
Try the problem first. Open one hint at a time.
The sum 1 + 2 + … + n is n × (n + 1) / 2.
Subtract the sum of the numbers present. Use long, because n × (n + 1) can pass the int limit.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor