Skip to content

1399. Count Largest Group #1597

Answered by mah-shamim
mah-shamim asked this question in Q&A
Apr 23, 2025 · 1 comments · 2 replies
Discussion options

You must be logged in to vote

We need to group numbers from 1 to n based on the sum of their digits and determine how many groups have the largest size.

Approach

  1. Calculate Digit Sums: For each number from 1 to n, compute the sum of its digits.
  2. Track Group Sizes: Use a hash map (associative array in PHP) to count how many numbers fall into each digit sum group.
  3. Determine Largest Group: Find the maximum size among all groups.
  4. Count Largest Groups: Count how many groups have this maximum size.

Let's implement this solution in PHP: 1399. Count Largest Group

<?php
/**
 * @param Integer $n
 * @return Integer
 */
function countLargestGroup($n) {
    $counts = array();
    for ($i = 1; $i <= $n; $i++) {
        $s = digitSum($

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@topugit
Comment options

topugit Apr 23, 2025
Collaborator

@mah-shamim
Comment options

mah-shamim Apr 23, 2025
Maintainer Author

Answer selected by topugit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested easy Difficulty
2 participants