Skip to content

Instantly share code, notes, and snippets.

@thmain
Created January 13, 2021 04:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thmain/fed1d519dfabba377b9125f01d77f05e to your computer and use it in GitHub Desktop.
Save thmain/fed1d519dfabba377b9125f01d77f05e to your computer and use it in GitHub Desktop.
import java.util.HashMap;
import java.util.Map;
public class CountLargestGroups {
public static int countLargestGroup(int n) {
Map<Integer, Integer> map =new HashMap<>();
int largestGrpSize = 0;
for(int i=n;i>0;i--){
int num = i;
int sum = 0;
while(num>0){
sum += num%10;
num = num/10;
}
int x = map.getOrDefault(sum, 0);
map.put(sum, x+1);
largestGrpSize = Math.max(largestGrpSize, x+1);
}
int countGrps = 0;
for(Integer i: map.keySet()){
if(map.get(i)==largestGrpSize)
countGrps++;
}
return countGrps;
}
public static void main(String[] args) {
System.out.println(countLargestGroup(15));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment