Created
January 13, 2021 04:30
-
-
Save thmain/fed1d519dfabba377b9125f01d77f05e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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