Q49 X of a Kind in a Deck of Cards - LeetCode
Mon Jan 30 2023 13:20:03 GMT+0000 (Coordinated Universal Time)
Saved by
@Ayush_dabas07
class Solution {
public boolean hasGroupsSizeX(int[] deck) {
//make a frequency map
HashMap<Integer,Integer> fmap = new HashMap<>();
for(int val : deck)
fmap.put(val,fmap.getOrDefault(val,0)+1);
//iterate the frequency map and find GCD(greatest common divisor) if found then
//that means they can be divide in GCD groups
int ans = 0;
for(int val : fmap.keySet()){
ans = gcd(ans,fmap.get(val));
}
//if gcd is 1 there is no common divisor except 1
return ans == 1 ? false : true;
}
public int gcd(int a , int b){
if(b == 0)
return a;
return gcd(b , a%b);
}
}
content_copyCOPY
https://leetcode.com/problems/x-of-a-kind-in-a-deck-of-cards/
Comments