- Related Questions & Answers
- Check if array sum can be made K by three operations on it in Python
- Check if given string can be formed by concatenating string elements of list in Python
- Check if a prime number can be expressed as sum of two Prime Numbers in Python
- Check if elements of an array can be arranged satisfying the given condition in Python
- Check if a two-character string can be made using given words in Python
- Count of numbers which can be made power of 2 by given operation in C++
- Check if some elements of array are equal JavaScript
- Check if LCM of array elements is divisible by a prime number or not in Python
- Maximum elements that can be made equal with k updates in C++
- Check if product of array containing prime numbers is a perfect square in Python
- Count of distinct sums that can be obtained by adding prime numbers from given arrays in C++
- Check if an array contains all elements of a given range in Python
- Check if the array can be sorted using swaps between given indices only in Python
- Check if the given array can be reduced to zeros with the given operation performed given number of times in Python
- Check if the elements of the array can be rearranged to form a sequence of numbers or not in JavaScript

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose we have two arrays, one is nums and another one is primes. We have to check whether it is possible to make all the elements of nums equal by multiplying one or more prime numbers from primes array.

So, if the input is like nums = [25, 100] primes = [2, 5], then the output will be True as we can multiply 25 with 2 twice to get 100 then all elements are same.

To solve this, we will follow these steps −

- lcm_arr := LCM of all elements in nums
- for i in range 0 to size of nums - 1, do
- val := lcm_arr/nums[i]
- if size of primes is not 0 and val is not 1, then
- while val mod primes[0] is 0, do
- val := val/primes[j]

- while val mod primes[0] is 0, do
- if val is not same as 1, then
- return False

- return True

Let us see the following implementation to get better understanding −

from math import gcd def array_lcm(nums): ans = nums[0] for i in range(1,len(nums)): ans = (nums[i]*ans)/gcd(nums[i], ans) return ans def solve(nums, primes): lcm_arr = array_lcm(nums) for i in range(len(nums)): val = lcm_arr/nums[i] for j in range(len(primes) and val != 1): while (val % primes[j] == 0): val = val/primes[j] if (val != 1): return False return True nums = [25, 100] primes = [2, 5] print(solve(nums, primes))

[25, 100], [2, 5]

True

Advertisements