Suppose we have an array nums, we have to check whether this array is containing all of the divisors of some integer or not.
So, if the input is like nums = [1, 2, 3, 4, 6, 8, 12, 24], then the output will be True as these are the divisors of 24.
To solve this, we will follow these steps −
Let us see the following implementation to get better understanding −
from math import sqrt 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 nums = [1, 2, 3, 4, 6, 8, 12, 24] print(solve(nums))
[1, 2, 3, 4, 6, 8, 12, 24]
True