Skip to content

Create XofaKindinaDeckofCards.java #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
Loading