The Instructor's Seat Mrs. Geeta Rao manages the ...
Created on: July 4, 2025
Created on: July 4, 2025
The Instructor's Seat
Mrs. Geeta Rao manages the UNIVERSITY classroom seating arrangements. The classroom has an infinite number of seats.
One day, a finite number of students come to attend a lecture.
The students consist of:
An Instructor.
→An unknown number of groups consisting of S members per group where S≠1.
The Instructor was given a separate seat, and the rest were assigned one seat per group.
Mrs. Geeta has a list of randomly arranged seat entries. The seat numbers will appear S times per group except for the Instructor's seat (which will occur only once). She needs you to help her find the Instructor's seat number.
Input Format
The first line contains an integer n, representing the number of students.
The second line consists of an integer, S, the size of each group.
Next n lines contains one integer per line representing seät number.
Output Format
Print the Instructor's seat number.
write code in python 3.9
Here’s an efficient and concise Python 3.9 solution.
It follows the trick used in HackerRank’s Captain’s Room problem: compare the sum of the unique seat numbers (multiplied by S) to the total sum. The difference isolates the Instructor’s seat.
python# The Instructor's Seat # Python 3.9 import sys def main() -> None: data = sys.stdin.read().strip().split() if not data: return # no input n = int(data[0]) # number of students (not actually needed) S = int(data[1]) # group size seats = list(map(int, data[2:])) unique_seats = set(seats) # Formula: (S * sum(unique) - sum(all)) // (S - 1) instructor_seat = (S * sum(unique_seats) - sum(seats)) // (S - 1) print(instructor_seat) if __name__ == "__main__": main()
Why this works
S × (sum of unique group seats)
double-counts every group seat compared with the real total.sum(seats)
leaves (S – 1)
copies of every group seat plus one copy of the Instructor’s seat.(S – 1)
cancels out the extra multiplicity, yielding the Instructor’s seat number in O(n) time and O(u) memory (u
= number of distinct seats).for above question give using def main fucntion
Here’s the same solution wrapped in a main()
function, ready for most online-judge runners:
python# Instructor's Seat # Python 3.9+ import sys def main() -> None: """ Reads: n – total number of students (ignored after reading) S – size of each group (S ≠ 1) n seat numbers, one per line Prints: The Instructor's seat number. """ data = sys.stdin.read().strip().split() if not data: return # no input provided n = int(data[0]) # total students (not needed for logic) S = int(data[1]) # group size seats = list(map(int, data[2:])) unique_seats = set(seats) # Each group seat appears S times; instructor seat appears once. instructor_seat = (S * sum(unique_seats) - sum(seats)) // (S - 1) print(instructor_seat) if __name__ == "__main__": main()
Explanation of the formula
x
, the expression S × sum(unique_seats)
counts x
exactly S
times.sum(seats)
leaves (S – 1)
copies of each group seat plus one copy of the Instructor’s seat.(S – 1)
removes the excess multiplicity, isolating the Instructor’s seat number in O(n) time and O(u) memory (u
= number of distinct seat numbers).