Saturday, May 10, 2025

Discussion about Favourite Singer (Problem from HackerEarth)

To See All Articles About Technology: Index of Lessons in Technology
Try out the problem here

Problem

Bob has a playlist of N songs, each song has a singer associated with it (denoted by an integer)

Favourite singer of Bob is the one whose songs are the most on the playlist

Count the number of Favourite Singers of Bob

Input Format 

The first line contains an integer N, denoting the number of songs in Bob's playlist.

The following input contains N integers, ith integer denoting the singer of the ith song.

Output Format

Output a single integer, the number of favourite singers of Bob

Note: Use 64 bit data type

Constraints
N <= 2*10^5
a[i] <= 10^15

Sample Input
5
1 1 2 2 4

Sample Output
2

Let's break this down and then provide a clear and efficient solution.


Problem Summary

  • You're given a list of integers representing singers for each song in Bob's playlist.

  • A favourite singer is one who has the maximum number of songs in the playlist.

  • You need to count how many singers have that maximum number of songs.


Approach

  1. Use a dictionary (or collections.Counter) to count how many songs each singer has.

  2. Find the maximum song count from this dictionary.

  3. Count how many singers have this maximum count.


Python Code

python
def count_favourite_singers(n, singers): from collections import defaultdict singer_count = defaultdict(int) # Step 1: Count songs for each singer for singer in singers: singer_count[singer] += 1 # Step 2: Find maximum count max_count = max(singer_count.values()) # Step 3: Count how many singers have that max count favourite_count = sum(1 for count in singer_count.values() if count == max_count) return favourite_count

Input/Output Handling

python
# Read input n = int(input()) singers = list(map(int, input().split())) # Compute and print the result print(count_favourite_singers(n, singers))

Time and Space Complexity

  • Time Complexity: O(n) – one pass to count, one pass to find max and count again.

  • Space Complexity: O(k) – where k is the number of unique singers.


Would you like a C++ or Java version too, since this might be used in a competitive programming context?




Can you do a comparison of your code with this code:

from collections import Counter

N = int(input())
l = [int(i) for i in input().split()]

most_common = Counter(l).most_common()

favorite_count = most_common[0][1]

m = [i for i in most_common if i[1] == favorite_count]

print(len(m))

Absolutely — let’s compare your code using Counter.most_common() with my code using defaultdict.


🔍 Code Comparison: Side by Side

Your Code

python
from collections import Counter N = int(input()) l = [int(i) for i in input().split()] most_common = Counter(l).most_common() # List of (singer, count) sorted descending favorite_count = most_common[0][1] # Get the max count m = [i for i in most_common if i[1] == favorite_count] # Filter singers with max count print(len(m)) # Count them

My Code

python
from collections import defaultdict def count_favourite_singers(n, singers): singer_count = defaultdict(int) for singer in singers: singer_count[singer] += 1 max_count = max(singer_count.values()) favourite_count = sum(1 for count in singer_count.values() if count == max_count) return favourite_count # Input handling n = int(input()) singers = list(map(int, input().split())) print(count_favourite_singers(n, singers))

✅ Key Differences

FeatureYour Code (Counter)My Code (defaultdict)
LibraryUses Counter.most_common()Uses manual counting with defaultdict
Sorting StepYes.most_common() returns a sorted listNo – avoids sorting
Time ComplexityO(n log n) due to sortingO(n) — more efficient
Space ComplexityO(k) for storing countsO(k) as well
ReadabilityShort and conciseSlightly more verbose, but very clear
FlexibilityLess general (tied to sorting order)More general and customizable

⏱️ Efficiency Note

The biggest difference is:

  • Counter.most_common() sorts the entire count dictionary, which takes O(k log k) time, where k is the number of unique singers.

  • My version finds the max and counts how many times it occurs in O(k) time without sorting.

In large datasets (e.g. 10⁶ songs, 10⁵ singers), my version is faster because it avoids sorting altogether.


🏁 Final Verdict

Use CaseRecommended Code
Small input (e.g. coding contest)Your code (short & clean)
Large input / time-sensitiveMy code (no sort, O(n))
Readability / beginner-friendlyMy code (step-by-step)

Would you like me to rewrite your version to avoid sorting but keep it just as concise?

Tags: Technology,Algorithms,

No comments:

Post a Comment