使用给定的 LCM 查找 Array 的子集
给定一个由N个正整数和一个正整数X组成的数组arr[] ,任务是找到给定数组的子集,其最低公倍数 (LCM) 为X。如果不存在任何子集,则打印“-1” 。
例子:
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
朴素方法:解决给定问题的最简单方法是找到给定数组的所有可能子集,如果存在 LCM 为X的任何子集,则打印该子集。否则,打印“-1” 。
时间复杂度: O(N*(log N)*2 N )
辅助空间: O(1)
有效方法:上述方法也可以通过使用以下事实进行优化:如果数组元素不是X的除数,则该元素不能包含在子集中,因为 LCM 永远不会是X 。请按照以下步骤解决问题:
- 初始化一个变量,例如LCM为1 ,它存储结果子集的 LCM。
- 初始化一个向量,比如subset[]来存储包含在结果子集中的数组元素。
- 遍历给定的数组arr[]并执行以下步骤:
- 如果当前元素是X的除数,则推入子集中的元素并取 LCM 的值为LCM 。
- 否则,继续迭代。
- 完成上述步骤后,如果LCM的值为X ,则打印数组子集 []作为结果子集。否则,打印“-1” 。
下面是上述方法的实现:
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[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 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
输出:
2 4 5
时间复杂度: O(N*log M),其中 M 是数组的最大元素。
辅助空间: O(N)