Find the subset of Array with given LCM
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++
#include <bits/stdc++.h>
using namespace std;
int LCM( int P, int Q)
{
return ((P * Q) / __gcd(P, Q));
}
int subsetArrayWithLCM_X( int A[], int N,
int X)
{
int lcm = 1;
vector< int > subset;
for ( int i = 0; i < N; i++) {
if (X % A[i] == 0) {
subset.push_back(A[i]);
lcm = LCM(lcm, A[i]);
}
}
if (X != lcm) {
cout << "-1" ;
return 0;
}
for ( int i = 0; i < subset.size(); i++) {
cout << subset[i] << ' ' ;
}
return 0;
}
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
import java.util.*;
class GFG {
static int LCM( int P, int Q)
{
return ((P * Q) / __gcd(P, Q));
}
static int subsetArrayWithLCM_X( int A[], int N, int X)
{
int lcm = 1 ;
Vector<Integer> subset = new Vector<Integer>();
for ( int i = 0 ; i < N; i++) {
if (X % A[i] == 0 ) {
subset.add(A[i]);
lcm = LCM(lcm, A[i]);
}
}
if (X != lcm) {
System.out.print( "-1" );
return 0 ;
}
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);
}
public static void main(String[] args) {
int arr[] = { 2 , 3 , 4 , 5 };
int X = 20 ;
int N = arr.length;
subsetArrayWithLCM_X(arr, N, X);
}
}
|
Python3
from math import gcd
def LCM(P, Q):
return ((P * Q) / / gcd(P, Q))
def subsetArrayWithLCM_X(A, N, X):
lcm = 1
subset = []
for i in range (N):
if (X % A[i] = = 0 ):
subset.append(A[i])
lcm = LCM(lcm, A[i])
if (X ! = lcm):
print ( "-1" )
return 0
for i in range ( len (subset)):
print (subset[i],end = ' ' )
return 0
if __name__ = = '__main__' :
arr = [ 2 , 3 , 4 , 5 ]
X = 20
N = len (arr)
subsetArrayWithLCM_X(arr, N, X)
|
C#
using System;
using System.Collections.Generic;
public class GFG {
static int LCM( int P, int Q)
{
return ((P * Q) / __gcd(P, Q));
}
static int subsetArrayWithLCM_X( int []A, int N, int X)
{
int lcm = 1;
List< int > subset = new List< int >();
for ( int i = 0; i < N; i++) {
if (X % A[i] == 0) {
subset.Add(A[i]);
lcm = LCM(lcm, A[i]);
}
}
if (X != lcm) {
Console.Write( "-1" );
return 0;
}
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);
}
public static void Main(String[] args) {
int []arr = { 2, 3, 4, 5 };
int X = 20;
int N = arr.Length;
subsetArrayWithLCM_X(arr, N, X);
}
}
|
Javascript
<script>
function gcd(a, b)
{
if (a == 0)
return b;
if (b == 0)
return a;
if (a == b)
return a;
if (a > b)
return gcd(a - b, b);
return gcd(a, b - a);
}
function LCM(P, Q)
{
return ((P * Q) / gcd(P, Q));
}
function subsetArrayWithLCM_X(A, N, X)
{
let lcm = 1;
let subset = [];
for (let i = 0; i < N; i++)
{
if (X % A[i] == 0)
{
subset.push(A[i]);
lcm = LCM(lcm, A[i]);
}
}
if (X != lcm)
{
document.write( "-1" );
return 0;
}
for (let i = 0; i < subset.length; i++)
{
document.write(subset[i] + ' ' );
}
return 0;
}
let arr = [ 2, 3, 4, 5 ];
let X = 20;
let N = arr.length;
subsetArrayWithLCM_X(arr, N, X);
</script>
|
Time Complexity: O(N*log M), where M is the maximum element of the array.
Auxiliary Space: O(N)
Last Updated :
22 Nov, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...