 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check if the given array contains all the divisors of some integer in Python
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 −
- maximum := maximum of nums
- temp := a new list
- for i in range 1 to square root of maximum, do- if maximum is divisible by i, then- insert i at the end of temp
- if quotient of (maximum / i) is not same as i, then- insert quotient of (maximum / i) at the end of temp
 
 
- if size of temp is not same as size of nums, then- return False
 
- sort the list nums and temp
- for i in range 0 to size of nums - 1, do- if temp[i] is not same as nums[i], then- return False
 
 
- if temp[i] is not same as nums[i], then
 
- if maximum is divisible by i, then
- return True
Let us see the following implementation to get better understanding −
Example Code
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))
Input
[1, 2, 3, 4, 6, 8, 12, 24]
Output
True
Advertisements
 