20. Group Balls by Sequence
✅ GFG solution to the Group Balls by Sequence problem: determine if balls can be grouped into consecutive sequences of length k using frequency mapping. 🚀
The problem can be found at the following link: 🔗 Question Link
🧩 Problem Description
You are given an array arr[]
of positive integers, where each element arr[i]
represents the number written on the i-th ball, and a positive integer k
. Your task is to determine whether it is possible to rearrange all the balls into groups such that:
Each group contains exactly k balls.
The numbers in each group are consecutive integers.
📘 Examples
Example 1
Input: arr[] = [10, 1, 2, 11], k = 2
Output: true
Explanation: The balls can be rearranged as [1, 2], [10, 11].
There are two groups of size 2. Each group has 2 consecutive numbers.
Example 2
Input: arr[] = [7, 8, 9, 10, 11], k = 2
Output: false
Explanation: The balls cannot be rearranged into groups of 2,
since there are 5 balls, and 5 balls cannot be divided into groups of 2.
🔒 Constraints
$1 \le \text{arr.size()} \le 10^6$
$0 \le \text{arr}[i] \le 10^5$
$1 \le k \le 10^3$
✅ My Approach
The optimal approach uses Frequency Mapping with Greedy Algorithm:
Frequency Map + Greedy Processing
Check Divisibility:
First, verify if the total number of balls is divisible by
k
.If not, return
false
immediately.
Build Frequency Map:
Use a
TreeMap
(or sorted map) to store frequency of each number.TreeMap ensures we process numbers in ascending order.
Greedy Group Formation:
For each number with non-zero frequency, try to form consecutive groups.
Starting from current number, check if next
k-1
consecutive numbers exist.Deduct the required frequency from all numbers in the sequence.
Validation:
If any consecutive number has insufficient frequency, return
false
.Continue until all numbers are processed.
📝 Time and Auxiliary Space Complexity
Expected Time Complexity: O(n log n + n _ k), where n is the array size. Building the frequency map takes O(n log n) time, and processing each element with k consecutive checks takes O(n _ k) time.
Expected Auxiliary Space Complexity: O(n), as we use a frequency map to store at most n distinct elements.
🧑💻 Code (C++)
class Solution {
public:
bool validgroup(vector<int> &arr, int k) {
map<int, int> m;
for (int x : arr) m[x]++;
for (auto& p : m) {
int v = p.first, f = p.second;
if (f == 0) continue;
for (int i = 1; i < k; i++) {
if (m[v + i] < f) return false;
m[v + i] -= f;
}
}
return true;
}
};
🧑💻 Code (Java)
class Solution {
public boolean validgroup(int[] arr, int k) {
Map<Integer, Integer> m = new TreeMap<>();
for (int x : arr) m.put(x, m.getOrDefault(x, 0) + 1);
for (Map.Entry<Integer, Integer> e : m.entrySet()) {
int v = e.getKey(), f = e.getValue();
if (f == 0) continue;
for (int i = 1; i < k; i++) {
if (m.getOrDefault(v + i, 0) < f) return false;
m.put(v + i, m.get(v + i) - f);
}
}
return true;
}
}
🐍 Code (Python)
from collections import Counter
class Solution:
def validgroup(self, arr, k):
m = Counter(arr)
for v in sorted(m.keys()):
f = m[v]
if f == 0: continue
for i in range(1, k):
if m[v + i] < f: return False
m[v + i] -= f
return True
🧠 Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: 📬 Any Questions?. Let's make this learning journey more collaborative!
⭐ If you find this helpful, please give this repository a star! ⭐
📍Visitor Count
Last updated