Check if the given array contains all the divisors of some integer in Python

Sometimes we need to verify if a given array contains all the divisors of some integer. This problem involves finding divisors of the maximum element and comparing them with the array elements.

So, if the input is like nums = [1, 2, 3, 4, 6, 8, 12, 24], then the output will be True as these are all the divisors of 24.

Algorithm

To solve this, we will follow these steps ?

  • Find the maximum element in the array (potential number whose divisors we're checking)
  • Generate all divisors of this maximum number
  • Compare the generated divisors with the original array
  • Return True if they match exactly, False otherwise

Implementation

from math import sqrt

def solve(nums):
    maximum = max(nums)
    
    # Find all divisors of maximum
    temp = []
    for i in range(1, int(sqrt(maximum)) + 1):
        if maximum % i == 0:
            temp.append(i)
            # Add the corresponding divisor (maximum // i)
            if (maximum // i != i):
                temp.append(maximum // i)
    
    # Check if count matches
    if len(temp) != len(nums):
        return False
    
    # Sort both arrays and compare
    nums.sort()
    temp.sort()
    
    for i in range(len(nums)):
        if temp[i] != nums[i]:
            return False
    return True

# Test with example
nums = [1, 2, 3, 4, 6, 8, 12, 24]
print(solve(nums))
True

How It Works

The algorithm works by:

  • Finding the maximum: The largest element is the potential number whose divisors we're checking
  • Generating divisors: We iterate up to ?maximum to find all divisor pairs efficiently
  • Avoiding duplicates: For perfect squares, we ensure the square root isn't added twice
  • Comparison: After sorting both arrays, we compare element by element

Example with Different Input

def solve(nums):
    maximum = max(nums)
    
    temp = []
    for i in range(1, int(sqrt(maximum)) + 1):
        if maximum % i == 0:
            temp.append(i)
            if (maximum // i != i):
                temp.append(maximum // i)
    
    if len(temp) != len(nums):
        return False
    
    nums.sort()
    temp.sort()
    
    for i in range(len(nums)):
        if temp[i] != nums[i]:
            return False
    return True

# Test cases
test1 = [1, 2, 3, 6]  # Divisors of 6
test2 = [1, 2, 4]     # Divisors of 4
test3 = [1, 2, 5]     # Not all divisors of any number

print(f"[1, 2, 3, 6]: {solve(test1)}")
print(f"[1, 2, 4]: {solve(test2)}")
print(f"[1, 2, 5]: {solve(test3)}")
[1, 2, 3, 6]: True
[1, 2, 4]: True
[1, 2, 5]: False

Time Complexity

The time complexity is O(?n + k log k) where n is the maximum element and k is the length of the array. The ?n comes from finding divisors, and k log k from sorting.

Conclusion

This solution efficiently checks if an array contains all divisors of some integer by finding divisors of the maximum element and comparing sorted arrays. The key insight is that if the array contains all divisors, the maximum element must be the target number.

Updated on: 2026-03-25T14:33:05+05:30

158 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements