Find the subset of Array with given LCM

• Difficulty Level : Hard
• Last Updated : 22 Nov, 2021

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 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 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);     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 subset = new Vector();           // 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 subset = new List();           // 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



Output:

2 4 5

Time Complexity: O(N*log M), where M is the maximum element of the array.
Auxiliary Space: O(N)

My Personal Notes arrow_drop_up
Recommended Articles
Page :