Find the subset of Array with given LCM
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:
- 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>
Time Complexity: O(N*log M), where M is the maximum element of the array.
Auxiliary Space: O(N)
Similar Reads
Find the Largest divisor Subset in the Array Given an array arr[] of N integers, the task is to find the largest subset of arr[] such that in every pair of numbers from that subset, one number is a divisor of the other. Examples: Input: arr[] = {1, 2, 3, 4, 5} Output: 4 2 1 All possible pairs of the subsequence are: (4, 2) -> 4 % 2 = 0 (4,
14 min read
Array with GCD of any of its subset belongs to the given array Given a set of N elements such that N\in [1, 1000] , task is to generate an array such that the GCD of any subset of the generated array lies in the given set of elements. The generated array should not be more than thrice the length of the set of the GCD. Prerequisite : GCD of an Array | Subset of
8 min read
Count Subarray of size K in given Array with given LCM Given an array arr[] of length N, the task is to find the number of subarrays where the least common multiple (LCM) of the subarray is equal to K and the size of that subarray is S. Examples: Input: arr[] = {1, 2, 3, 4, 5, 6}, K = 6, S = 2Output: 1Explanation: {1, 2, 3 }, {2, 3}, {6}There are 3 suba
8 min read
Find the GCD of LCM of all unique pairs in an Array Given an integer array arr[] of size N, the task is to find the GCD of LCM of all unique pair (i, j) of the array, such that i < j.Examples: Input: arr[] = {10, 24, 40, 80} Output: 40 Explanation: LCM of all unique pairs following given conditions are: LCM(10, 24) = 120 LCM(10, 40) = 40 LCM(10, 8
9 min read
LCM of given array elements In this article, we will learn how to find the LCM of given array elements.Given an array of n numbers, find the LCM of it. Example:Input : {1, 2, 8, 3}Output : 24LCM of 1, 2, 8 and 3 is 24Input : {2, 7, 3, 9, 4}Output : 252Table of Content[Naive Approach] Iterative LCM Calculation - O(n * log(min(a
14 min read