The Captains Room in python - HackerRank Solution

The Captains Room in python - HackerRank Solution
The Captains Room in python - HackerRank Solution


Problem :


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 ≠ 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.



Solution :


# The Captains Room in python - Hacker Rank Solution
# Python 3
# Enter your code here. Read input from STDIN. Print output to STDOUT
# The Captains Room in python - Hacker Rank Solution START

N = int(input())

storage = map(int, input().split())
storage = sorted(storage)

for i in range(len(storage)):
    if(i != len(storage)-1):
        if(storage[i]!=storage[i-1] and storage[i]!=storage[i+1]):
            print(storage[i])
            break;
    else:
        print(storage[i])
# The Captains Room in python - Hacker Rank Solution END





Disclaimer :-
the above hole problem statement is given by hackerrank.com but the solution is generated by the codeworld19 authority if any of the query regarding this post or website fill the following contact form thank you.

Next Post Previous Post
4 Comments
  • Unknown
    Unknown Tuesday, December 28, 2021

    from collections import Counter
    n=int(input())
    x=Counter(list(map(int,input().split())))
    for i in x:
    if x[i]==1:
    print(i)

  • Unknown
    Unknown Saturday, February 19, 2022

    K=int(input())
    rn=tuple(map(int,input().split()))
    for i in range(len(rn)):
    if rn[i] not in rn[:i]:
    c=rn[i]
    print(c)

  • Unknown
    Unknown Thursday, February 24, 2022

    k=int(input())
    x=list(map(int,input().split()))
    y=list(set(x))
    for i in y:
    count=0
    for j in x:
    if i==j:
    count=count+1
    if count==1 :
    print (i)

    # alternate soln.
    k=int(input())
    x=list(map(int,input().split()))
    for i in x:
    if x.count(i)==1:
    print(i)

  • Gaurav Sahane
    Gaurav Sahane Tuesday, April 05, 2022

    k=int(input())
    q=list(map(int,input().split()))
    dic={}
    for i in q:
    if(i in dic):
    dic[i]+=1
    else:
    dic[i]=1
    test=list(dic.values())[0]
    for j in dic:
    if(dic[j]!=test):
    print(j)
    break

Add Comment
comment url