Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
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:
x cards where x > 1, andReturn true if such partition is possible, or false otherwise.
Example 1:
Input: deck = [1,2,3,4,4,3,2,1] Output: true Explanation: Possible partition [1,1],[2,2],[3,3],[4,4].
Example 2:
Input: deck = [1,1,1,2,2,2,3,3] Output: false Explanation: No possible partition.
Constraints:
1 <= deck.length <= 1040 <= deck[i] < 104Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map · Math
[1,2,3,4,4,3,2,1]
[1,1,1,2,2,2,3,3]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #914: X of a Kind in a Deck of Cards
class Solution {
public boolean hasGroupsSizeX(int[] deck) {
Map<Integer, Integer> cnt = new HashMap<>();
for (int x : deck) {
cnt.merge(x, 1, Integer::sum);
}
int g = cnt.get(deck[0]);
for (int x : cnt.values()) {
g = gcd(g, x);
}
return g >= 2;
}
private int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
}
// Accepted solution for LeetCode #914: X of a Kind in a Deck of Cards
func hasGroupsSizeX(deck []int) bool {
cnt := map[int]int{}
for _, x := range deck {
cnt[x]++
}
g := cnt[deck[0]]
for _, x := range cnt {
g = gcd(g, x)
}
return g >= 2
}
func gcd(a, b int) int {
if b == 0 {
return a
}
return gcd(b, a%b)
}
# Accepted solution for LeetCode #914: X of a Kind in a Deck of Cards
class Solution:
def hasGroupsSizeX(self, deck: List[int]) -> bool:
cnt = Counter(deck)
return reduce(gcd, cnt.values()) >= 2
// Accepted solution for LeetCode #914: X of a Kind in a Deck of Cards
struct Solution;
use std::collections::HashMap;
impl Solution {
fn gcd(a: i32, b: i32) -> i32 {
if a == 0 {
b
} else {
Self::gcd(b % a, a)
}
}
fn has_groups_size_x(deck: Vec<i32>) -> bool {
let mut hm: HashMap<i32, i32> = HashMap::new();
let mut max = 0;
for x in deck {
let count = hm.entry(x).or_default();
*count += 1;
}
for &v in hm.values() {
max = Self::gcd(max, v);
}
max >= 2
}
}
#[test]
fn test() {
let deck = vec![1, 2, 3, 4, 4, 3, 2, 1];
assert_eq!(Solution::has_groups_size_x(deck), true);
let deck = vec![1, 1, 1, 2, 2, 2, 3, 3];
assert_eq!(Solution::has_groups_size_x(deck), false);
let deck = vec![1];
assert_eq!(Solution::has_groups_size_x(deck), false);
let deck = vec![1, 1];
assert_eq!(Solution::has_groups_size_x(deck), true);
let deck = vec![1, 1, 2, 2, 2, 2];
assert_eq!(Solution::has_groups_size_x(deck), true);
}
// Accepted solution for LeetCode #914: X of a Kind in a Deck of Cards
function hasGroupsSizeX(deck: number[]): boolean {
const cnt: Record<number, number> = {};
for (const x of deck) {
cnt[x] = (cnt[x] || 0) + 1;
}
const gcd = (a: number, b: number): number => (b === 0 ? a : gcd(b, a % b));
let g = cnt[deck[0]];
for (const [_, x] of Object.entries(cnt)) {
g = gcd(g, x);
}
return g >= 2;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.