Thursday, November 14, 2019

The Captain's Room - Hacker Rank 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  members per group where  ≠ .
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  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  and the room number list.
Input Format
The first line consists of an integer, , the size of each group.
The second line contains the unordered elements of the room number list.
Constraints
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  elements. Since  is , there must be  groups of families. In the given list, all of the numbers repeat  times except for room number .
Hence,  is the Captain's room number.

The Captain's Room - Hacker Rank Solution

The approach to solve this problem follows:
1. Store List: Store the list in a variable. Let roomList be the variable storing the list of room numbers.
2. Room Set: In the variable roomSet, store the set of roomList.
3. Sum Room Set and List: In the variable sumRoomSet and sumRoomList, store the summations of roomSet and roomList, respectively.
4. Multiply K and Subtract: Now, we multiply  with sumRoomSet, subtract the sumRoomList from it and then store the result in the variable temp. Therefore, temp = the Captain's room number  .
5. Divide by K-1: Divide temp by  and store the result in the variable answer.
6. Output: Print answer.

K = int(raw_input())

#Step 1
roomList = map(int,raw_input().split())

#Step 2
roomSet = set(roomList) 

#Step 3
sumRoomSet = sum(roomSet) 
sumRoomList = sum(roomList) 

# Step 4
temp = sumRoomSet * K - sumRoomList 

# Step 5
answer = temp / (K - 1) 

# Step 6
print answer
K = int(input())
set_S = set()
sumlist_S = 0
for i in input().split():
    I = int(i)
    set_S.add(I)
    sumlist_S += I

print((sum(set_S)*K - sumlist_S)//(K-1))

4 comments:

  1. Thanks for the solution. But, Can you help me explain the mathematical meaning of the question. I feel it will help in understanding it

    ReplyDelete
  2. k,arr = int(input()),list(map(int, input().split()))
    myset = set(arr)

    print(((sum(myset)*k)-(sum(arr)))//(k-1))

    ReplyDelete
  3. # Solution of same problem

    from collections import Counter
    if __name__=='__main__':
    k = int(input())
    arr = list(map(int, input().split()))
    arr1 = dict(Counter(arr))

    arr3 = list(arr1.keys())
    arr4 = list(arr1.values())
    print(arr3[arr4.index(1)])

    ReplyDelete
  4. Can someone explain the expression??

    ReplyDelete

Powered by Blogger.