Algorithm


Problem Name: Python - The Captain's Room

Problem Link: https://www.hackerrank.com/challenges/py-the-captains-room/problem?isFullScreen=true

In this HackerRank Functions in PYTHON problem solution,

Mr. Anant Asankhya is the manager at the INFINITE hotel. The hotel has an infinite amount of rooms.

One fine day, a finite number of tourists come to stay at the hotel.
The tourists consist of:
→ A Captain.
→ An unknown group of families consisting of k members per group where k not= 1.

The Captain was given a separate room, and the rest were given one room per group.

Mr. Anant has an unordered list of randomly arranged room entries. The list consists of the room numbers for all of the tourists. The room numbers will appear k times per group except for the Captain's room. Mr. Anant needs you to help him find the Captain's room number.
The total number of tourists or the total number of groups of families is not known to you.
You only know the value of k and the room number list.

Input Format

The first line consists of an integer, K , the size of each group.
The second line contains the unordered elements of the room number list.

Constraints

1 < K < 1000

Output Format

Output the Captain's room number.

Sample Input

5
1 2 3 6 5 4 4 2 5 3 6 1 6 5 3 2 4 1 2 5 1 4 3 6 8 4 3 1 5 6 2 

Sample Output

8

Explanation

The list of room numbers contains 31 elements. Since K is 5, there must be 6 groups of families. In the given list, all of the numbers repeat 5 times except for room number 8.

 

Hence, 8 is the Captain's room number.

 

 

 

Code Examples

#1 Code Example with Python Programming

Code - Python Programming


k = int(input())
frequency_map = {}
for i in map(int, input().split()):
    if i in frequency_map:
        frequency_map[i] += 1
    else:
        frequency_map[i] = 1
for i in frequency_map:
    if frequency_map[i] == 1:
        print(i)
Copy The Code & Try With Live Editor
Advertisements

Demonstration


Previous
[Solved] Set Mutations in PYTHON solution in Hackerrank
Next
[Solved] Check Subset in PYTHON solution in Hackerrank