Lost card. You have a set of cards numbered from 1 to N. One card is missing. Given the number N followed by N-1 distinct integers representing the numbers on the remaining cards, determine and print the number on the missing card.
|
N = int(input()) total_sum = N * (N + 1) // 2 remaining_sum = 0 for _ in range(N - 1): remaining_sum += int(input()) print(total_sum - remaining_sum) |
- Reading Input:
- The
input()
function reads a line of text from the user.
- The
int()
function converts this input to an integer and stores it in the variable N
.
- Calculating Total Sum of All Cards:
total_sum = N * (N + 1) // 2
- The total sum of the numbers from 1 to N is calculated using the formula N×(N+1)2\frac{N \times (N + 1)}{2}2N×(N+1).
- Calculating Sum of Remaining Cards:
|
remaining_sum = 0 for _ in range(N - 1): remaining_sum += int(input()) |
- Initialize
remaining_sum
to 0.
- Use a
for
loop to iterate N - 1
times, reading the remaining card numbers and summing them up.
- Finding the Missing Card:
print(total_sum - remaining_sum)
- The missing card number is found by subtracting
remaining_sum
from total_sum
.
- The result is printed.