Given an array arr[] consisting of N positive integers and a positive integer X, the task is to find the subset of the given array whose Lowest Common Multiple(LCM) is X. If there doesn’t exists any subset then print “-1”.
Examples:
Input: arr[ ] = {2, 4, 3, 5}, X = 20
Output: {4, 5}
Explanation:
Consider the subset {4, 5}, the LCM of this subset is 20(= X). Therefore, print this subset.Input: arr[ ] = {2, 3, 4, 5}, X = 18
Output: -1
Naive Approach: The simplest approach to solve the given problem is to find all possible subsets of the given array and if there exists any subset whose LCM is X, then print that subset. Otherwise, print “-1”.
Time Complexity: O(N*(log N)*2N)
Auxiliary Space: O(1)
Efficient Approach: The above approach can also be optimized by using the fact that if an array element is not a divisor of X, then that element can’t be included in the subset as the LCM will never be X. Follow the steps below to solve the problem:
- Initialize a variable, say LCM as 1 that stores the LCM of the resultant subset.
- Initialize a vector, say subset[] to store the array element included in the resultant subset.
- Traverse the given array arr[] and perform the following steps:
- If the current element is a divisor of X, then push the element in the subset and take LCM with the value LCM.
- Otherwise, continue the iteration.
- After completing the above steps, if the value LCM is X, then print the array subset[] as the resultant subset. Otherwise, print “-1”.
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to find the LCM of two // numbers P and Q int LCM( int P, int Q) { // Return the value of LCM return ((P * Q) / __gcd(P, Q)); } // Function to find the subset with // LCM as X int subsetArrayWithLCM_X( int A[], int N, int X) { // Stores LCM of resultant subset int lcm = 1; // Stores elements of subset vector< int > subset; // Traverse the array A[] for ( int i = 0; i < N; i++) { // Check if the current element // is a divisor of X if (X % A[i] == 0) { // Inserting it into subset subset.push_back(A[i]); // Update the lcm lcm = LCM(lcm, A[i]); } } // Check if the final LCM is // not equal to X if (X != lcm) { cout << "-1" ; return 0; } // Otherwise, print the subset for ( int i = 0; i < subset.size(); i++) { cout << subset[i] << ' ' ; } return 0; } // Driver Code int main() { int arr[] = { 2, 3, 4, 5 }; int X = 20; int N = sizeof (arr) / sizeof (arr[0]); subsetArrayWithLCM_X(arr, N, X); return 0; } |
Java
// Java program for the above approach import java.util.*; class GFG { // Function to find the LCM of two // numbers P and Q static int LCM( int P, int Q) { // Return the value of LCM return ((P * Q) / __gcd(P, Q)); } // Function to find the subset with // LCM as X static int subsetArrayWithLCM_X( int A[], int N, int X) { // Stores LCM of resultant subset int lcm = 1 ; // Stores elements of subset Vector<Integer> subset = new Vector<Integer>(); // Traverse the array A[] for ( int i = 0 ; i < N; i++) { // Check if the current element // is a divisor of X if (X % A[i] == 0 ) { // Inserting it into subset subset.add(A[i]); // Update the lcm lcm = LCM(lcm, A[i]); } } // Check if the final LCM is // not equal to X if (X != lcm) { System.out.print( "-1" ); return 0 ; } // Otherwise, print the subset for ( int i = 0 ; i < subset.size(); i++) { System.out.print(subset.get(i) + " " ); } return 0 ; } static int __gcd( int a, int b) { return b == 0 ? a : __gcd(b, a % b); } // Driver Code public static void main(String[] args) { int arr[] = { 2 , 3 , 4 , 5 }; int X = 20 ; int N = arr.length; subsetArrayWithLCM_X(arr, N, X); } } // This code is contributed by 29AjayKumar |
Python3
# Python 3 program for the above approach from math import gcd # Function to find the LCM of two # numbers P and Q def LCM(P, Q): # Return the value of LCM return ((P * Q) / / gcd(P, Q)) # Function to find the subset with # LCM as X def subsetArrayWithLCM_X(A, N, X): # Stores LCM of resultant subset lcm = 1 # Stores elements of subset subset = [] # Traverse the array A[] for i in range (N): # Check if the current element # is a divisor of X if (X % A[i] = = 0 ): # Inserting it into subset subset.append(A[i]) # Update the lcm lcm = LCM(lcm, A[i]) # Check if the final LCM is # not equal to X if (X ! = lcm): print ( "-1" ) return 0 # Otherwise, print the subset for i in range ( len (subset)): print (subset[i],end = ' ' ) return 0 # Driver Code if __name__ = = '__main__' : arr = [ 2 , 3 , 4 , 5 ] X = 20 N = len (arr) subsetArrayWithLCM_X(arr, N, X) # This code is contributed by SURENDRA_GANGWAR. |
C#
// C# program for the above approach using System; using System.Collections.Generic; public class GFG { // Function to find the LCM of two // numbers P and Q static int LCM( int P, int Q) { // Return the value of LCM return ((P * Q) / __gcd(P, Q)); } // Function to find the subset with // LCM as X static int subsetArrayWithLCM_X( int []A, int N, int X) { // Stores LCM of resultant subset int lcm = 1; // Stores elements of subset List< int > subset = new List< int >(); // Traverse the array []A for ( int i = 0; i < N; i++) { // Check if the current element // is a divisor of X if (X % A[i] == 0) { // Inserting it into subset subset.Add(A[i]); // Update the lcm lcm = LCM(lcm, A[i]); } } // Check if the readonly LCM is // not equal to X if (X != lcm) { Console.Write( "-1" ); return 0; } // Otherwise, print the subset for ( int i = 0; i < subset.Count; i++) { Console.Write(subset[i] + " " ); } return 0; } static int __gcd( int a, int b) { return b == 0 ? a : __gcd(b, a % b); } // Driver Code public static void Main(String[] args) { int []arr = { 2, 3, 4, 5 }; int X = 20; int N = arr.Length; subsetArrayWithLCM_X(arr, N, X); } } // This code is contributed by Princi Singh |
Javascript
<script> // JavaScript program for the above approach // Function to find the LCM of two // numbers P and Q function gcd(a, b) { // Everything divides 0 if (a == 0) return b; if (b == 0) return a; // Base case if (a == b) return a; // a is greater if (a > b) return gcd(a - b, b); return gcd(a, b - a); } // Function to find the LCM of two // numbers P and Q function LCM(P, Q) { // Return the value of LCM return ((P * Q) / gcd(P, Q)); } // Function to find the subset with // LCM as X function subsetArrayWithLCM_X(A, N, X) { // Stores LCM of resultant subset let lcm = 1; // Stores elements of subset let subset = []; // Traverse the array A[] for (let i = 0; i < N; i++) { // Check if the current element // is a divisor of X if (X % A[i] == 0) { // Inserting it into subset subset.push(A[i]); // Update the lcm lcm = LCM(lcm, A[i]); } } // Check if the final LCM is // not equal to X if (X != lcm) { document.write( "-1" ); return 0; } // Otherwise, print the subset for (let i = 0; i < subset.length; i++) { document.write(subset[i] + ' ' ); } return 0; } // Driver Code let arr = [ 2, 3, 4, 5 ]; let X = 20; let N = arr.length; subsetArrayWithLCM_X(arr, N, X); // This code is contributed by Potta Lokesh </script> |
2 4 5
Time Complexity: O(N*log M), where M is the maximum element of the array.
Auxiliary Space: O(N)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!