Given an array and a number k, the task is to find if LCM of the array is divisible by k or not.
Examples :
Input : int[] a = {10, 20, 15, 25}
k = 3
Output : true
Input : int[] a = {24, 21, 45, 57, 36};
k = 23;
Output : false
One simple solution is to first find LCM of array elements, then check if LCM is divisible by k or not.
Here, without calculating the LCM of the number we can find that LCM of the an array of number is divisible by a prime number k or not. If any number of the array is divisible by prime number k, then the LCM of the number is also divisible by prime number k.
C++
// C++ program to find LCM of // array of number is divisible // by a prime number k or not #include<iostream> using namespace std; // Function to check any number of // array is divisible by k or not bool func(int a[], int k, int n) { // If any array element is divisible // by k, then LCM of whole array // should also be divisible. for (int i = 0; i < n; i++) if (a[i] % k == 0) return true; return false; } // Driver Code int main() { int a[] = {14, 27, 38, 76, 84}; int k = 19; bool res = func(a, k, 5); if(res) cout<<"true"; else cout<<"false"; return 0; } // This code is contributed // by Mr. Somesh Awasthi |
Java
// Java program to find LCM of // array of number is divisible // by a prime number k or not import java.lang.*; import java.util.*; class GFG { // Function to check any number // of array is divisible by k or not static boolean func( int a[], int k) { // If any array element is divisible // by k, then LCM of whole array // should also be divisible. for (int i = 0; i < a.length; i++) if (a[i] % k == 0) return true; return false; } // Driver Code public static void main(String args[]) { int[] a = {14, 27, 38, 76, 84}; int k = 19; boolean res = func(a, k); System.out.println(res); } } |
Python 3
# Python 3 program to find LCM # of array of number is divisible # by a prime number k or not # Function to check any number of # array is divisible by k or not def func( a, k, n) : # If any array element is # divisible by k, then LCM # of whole array should also # be divisible. for i in range(0, n) : if ( a[i] % k == 0): return True # Driver Code a = [14, 27, 38, 76, 84] k = 19res = func(a, k, 5) if(res) : print("true") else : print("false") # This code is contributed # by Nikita Tiwari. |
C#
// C# program to find LCM of array // of number is divisible by a prime // number k or not using System; class GFG { // Function to check any number of // array is divisible by k or not static bool func(int []a, int k) { // If any array element is // divisible by k, then LCM // of whole array should also // be divisible. for (int i = 0; i < a.Length; i++) if (a[i] % k == 0) return true; return false; } // Driver code public static void Main() { int []a = {14, 27, 38, 76, 84}; int k = 19; bool res = func(a, k); Console.Write(res); } } // This code is contributed by nitin mittal. |
PHP
<?php // PHP program to find LCM of // array of number is divisible // by a prime number k or not // Function to check any number of // array is divisible by k or not function func( $a, $k, $n) { // If any array element is divisible // by k, then LCM of whole array // should also be divisible. for ($i = 0; $i < $n; $i++) if ($a[$i] % $k == 0) return true; return false; } // Driver Code $a = array(14, 27, 38, 76, 84); $k = 19; $res = func($a, $k, 5); if($res) echo"true"; elseecho"false"; // This code is contributed by nitin mittal ?> |
Output :
true
This article is contributed by Pramod Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Sum of LCM(1, n), LCM(2, n), LCM(3, n), ... , LCM(n, n)
- Minimum replacement of pairs by their LCM required to reduce given array to its LCM
- Check whether the sum of prime elements of the array is prime or not
- Prime factors of LCM of array elements
- Smallest composite number not divisible by first N prime numbers
- Sum of largest divisor of numbers upto N not divisible by given prime number P
- Check if a number is divisible by all prime divisors of another number
- Count of elements not divisible by any other elements of Array
- Check if all array elements are pairwise co-prime or not
- Check if sum of count of digits of array elements is Prime or not
- Find permutation of n which is divisible by 3 but not divisible by 6
- Check if GCD of all Composite Numbers in an array divisible by K is a Fibonacci Number or not
- Program to find the LCM of two prime numbers
- Check if the sum of primes is divisible by any prime from the array
- Find prime factors of Array elements whose sum of exponents is divisible by K
- Check if a prime number can be expressed as sum of two Prime Numbers
- Number of ways to change the Array such that largest element is LCM of array
- Count of pairs upto N such whose LCM is not equal to their product for Q queries
- Check if any permutation of array contains sum of every adjacent pair not divisible by 3
- Check if a large number is divisible by 11 or not
Improved By : nitin mittal

