Solved LeetCode problem 914 with Java: X of a Kind in a Deck of Cards

🚀 Day 398 of #500DaysOfCode 🔹 Problem: 914. X of a Kind in a Deck of Cards 🔹 Difficulty: Easy 🔹 Language Used: Java ☕ 🧩 Problem Summary: Given a deck of cards where each card has an integer, the goal is to check if we can divide the deck into groups such that: Each group has exactly X cards, where X > 1, and All cards in a group have the same integer. If such a partition is possible, return true — otherwise, return false. 💡 Key Idea: The solution relies on finding the greatest common divisor (GCD) of all card frequencies. If the GCD of all counts is greater than 1, we can form valid groups; otherwise, we can’t. ⚙️ Approach: 1️⃣ Count the frequency of each card using a HashMap. 2️⃣ Compute the GCD of all frequency values. 3️⃣ If GCD > 1, return true; else, false. 🧠 Concepts Used: HashMap GCD (Euclidean Algorithm) Frequency Counting ✅ Example: Input: [1,2,3,4,4,3,2,1] → Output: true Input: [1,1,1,2,2,2,3,3] → Output: false 📘 Lesson Learned: Even simple-looking problems can hide elegant mathematical patterns. Understanding GCD turned out to be the key! 💪 #Day398 #Java #LeetCode #ProblemSolving #CodingChallenge #LearnEveryday #Programming #Developer #100DaysOfCode #500DaysOfCode

  • graphical user interface, text

To view or add a comment, sign in

Explore content categories