Group Integers in Python

In Python, you can group integers into equal-sized groups where all numbers in each group are identical. This involves checking if a list can be split into groups where each group has the same size (?2) and contains identical elements.

The solution uses the Greatest Common Divisor (GCD) to find if all frequencies share a common factor greater than 1.

Algorithm Steps

The approach follows these steps ?

  • Count frequency of each distinct element
  • Find GCD of all frequencies
  • If GCD > 1, grouping is possible
  • If GCD = 1, grouping is impossible

Implementation

Here's the complete solution ?

from collections import Counter
import math

class Solution:
    def solve(self, nums):
        counts = Counter(nums)
        temp = 0
        
        for count in counts:
            if temp == 0:
                temp = counts[count]
            else:
                temp = math.gcd(counts[count], temp)
                if temp == 1:
                    return False
        
        return True

# Test the solution
ob = Solution()
numbers = [3, 4, 6, 9, 4, 3, 6, 9]
result = ob.solve(numbers)
print(f"Can group {numbers}? {result}")
Can group [3, 4, 6, 9, 4, 3, 6, 9]? True

How It Works

Let's trace through the example step by step ?

from collections import Counter
import math

numbers = [3, 4, 6, 9, 4, 3, 6, 9]
counts = Counter(numbers)
print("Frequency count:", dict(counts))

# Find GCD of all frequencies
frequencies = list(counts.values())
print("Frequencies:", frequencies)

gcd_result = frequencies[0]
for freq in frequencies[1:]:
    gcd_result = math.gcd(gcd_result, freq)
    print(f"GCD so far: {gcd_result}")

print(f"Final GCD: {gcd_result}")
print(f"Can group: {gcd_result > 1}")
Frequency count: {3: 2, 4: 2, 6: 2, 9: 2}
Frequencies: [2, 2, 2, 2]
GCD so far: 2
GCD so far: 2
GCD so far: 2
Final GCD: 2
Can group: True

Different Examples

Let's test with cases that can and cannot be grouped ?

from collections import Counter
import math

def can_group_integers(nums):
    counts = Counter(nums)
    temp = 0
    
    for count in counts:
        if temp == 0:
            temp = counts[count]
        else:
            temp = math.gcd(counts[count], temp)
            if temp == 1:
                return False
    
    return temp > 1

# Test cases
test_cases = [
    [1, 1, 2, 2, 3, 3],          # Can group into 3 groups of size 2
    [1, 1, 1, 2, 2, 2],          # Can group into 2 groups of size 3
    [1, 1, 2, 2, 2],             # Cannot group (frequencies: 2, 3)
    [1, 2, 3, 4]                 # Cannot group (all frequencies are 1)
]

for i, case in enumerate(test_cases, 1):
    counts = Counter(case)
    result = can_group_integers(case)
    print(f"Case {i}: {case}")
    print(f"  Frequencies: {list(counts.values())}")
    print(f"  Can group: {result}")
    print()
Case 1: [1, 1, 2, 2, 3, 3]
  Frequencies: [2, 2, 2]
  Can group: True

Case 2: [1, 1, 1, 2, 2, 2]
  Frequencies: [3, 3]
  Can group: True

Case 3: [1, 1, 2, 2, 2]
  Frequencies: [2, 3]
  Can group: False

Case 4: [1, 2, 3, 4]
  Frequencies: [1, 1, 1, 1]
  Can group: False

Key Points

  • Each group must contain identical numbers
  • All groups must have the same size (?2)
  • GCD of frequencies determines if equal grouping is possible
  • Time complexity: O(n + k log max_freq) where k is unique elements

Conclusion

Use GCD of element frequencies to determine if integers can be grouped equally. If the GCD is greater than 1, equal-sized grouping is possible with each group containing identical elements.

Updated on: 2026-03-25T10:23:27+05:30

986 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements