Skip to content

Commit

Permalink
Create XofaKindinaDeckofCards.java
Browse files Browse the repository at this point in the history
  • Loading branch information
abhishektripathi66 authored Oct 30, 2024
1 parent 8c857eb commit ffada26
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions Leetcode/XofaKindinaDeckofCards.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
914. X of a Kind in a Deck of Cards
Solved
Easy
Topics
Companies
You are given an integer array deck where deck[i] represents the number written on the ith card.
Partition the cards into one or more groups such that:
Each group has exactly x cards where x > 1, and
All the cards in one group have the same integer written on them.
Return true if such partition is possible, or false otherwise.
*/
class Solution {
public boolean hasGroupsSizeX(int[] deck) {
if(deck.length<2) return false;
Map<Integer,Integer> a = new HashMap<>();
for(int i=0;i<deck.length;i++){
a.put(deck[i],a.getOrDefault(deck[i],0)+1);
}

int ans = 0;

for(int key: a.keySet()){

ans = gcd(ans, a.get(key));
}

return ans>=2 ? true : false;


}

public int gcd(int a, int b){
if(b==0){
return a ;
}
return gcd(b, a%b);
}
}

0 comments on commit ffada26

Please sign in to comment.