SWE Prep

Share this post

Count Largest Group

sweprep.substack.com

Count Largest Group

Lessons Learned: HashMaps

Sameer Khoja
Apr 23, 2021
Share this post

Count Largest Group

sweprep.substack.com

Question: Given an integer n. Each number from 1 to n is grouped according to the sum of its digits. 

Return how many groups have the largest size.

Example:

  • N = 13

  • Output: 4

  • Explanation: We arrive at 9 groups in total: [1, 10], [2,11], [3,12], [4,13], [5], [6], [7], [8], [9]. There are 4 groups with largest size.

Solution

Approach 1: HashMap

Our solution will take on the following steps:

  1. Calculate the sum of digits if the number is multidigit

  2. Keep a count of the key and the value (key = sum of digits, value = number)

  3. Keep a track of the max frequency count for the key

  4. Retrieve a list of all the values from the map and count the values which are the same as the max size.

See Approach 1

Share this post

Count Largest Group

sweprep.substack.com
TopNew

No posts

Ready for more?

© 2023 Sameer Khoja
Privacy ∙ Terms ∙ Collection notice
Start WritingGet the app
Substack is the home for great writing