Given an array arr[] consisting of N positive integers, the task is to check if the sum of the elements of any subset of the given array can be reduced to 1 after multiplying all its elements by any integer. If it is not possible to do so, then print “No”. Otherwise, print “Yes”.
Examples:
Input: arr[] = {29, 6, 4, 10}
Output: Yes
Explanation:
Choose a subset {29, 6, 10} and multiply each corresponding element by {1, -3, -1}.
Therefore, sum of the subset = 29 * (1) + 6 * (-3) + 10 * (-1) = 29 – 18 – 10 = 1.
Therefore, print “Yes”.Input: arr[] = {6, 3, 9}
Output: No
Naive Approach: The simplest approach is to generate all possible subsets of the given array and if there exists any subset in the array such that the sum of its elements, after being multiplied by any integer, results to 1, then print “Yes”. Otherwise, print “No”.
Time Complexity: O(N * 2N)
Auxiliary Space: O(1)
Efficient Approach: The above approach can also be optimized by using Bezout’s identity (Bezout’s lemma), which states that if the GCD of any two integers a and b is equal to d, then there exists integers x and y, such that a * x + b * y = d.
Therefore, the idea is to check if the GCD of the given array arr[] can be made 1 or not. Hence, to satisfy the given condition, there must exist any two elements whose GCD is 1, then the GCD of the array will be equal to 1. Hence, print “Yes”. Otherwise, print “No”.
Below is the implementation of the above approach:
// C++ program for the above approach #include <bits/stdc++.h> using namespace std;
// Function to return gcd of a and b int gcd( int a, int b)
{ // Base Case
if (a == 0)
return b;
// Find the GCD recursively
return gcd(b % a, a);
} // Function to calculate the GCD // of the array arr[] int findGCDofArray( int arr[], int N)
{ // Stores the GCD of array
int g = 0;
// Traverse the array arr[]
for ( int i = 0; i < N; i++) {
// Update gcd of the array
g = gcd(g, arr[i]);
// If gcd is 1, then return 1
if (g == 1) {
return 1;
}
}
// Return the resultant GCD
return g;
} // Function to check if a subset satisfying // the given condition exists or not void findSubset( int arr[], int N)
{ // Calculate the gcd of the array
int gcd = findGCDofArray(arr, N);
// If gcd is 1, then print Yes
if (gcd == 1) {
cout << "Yes" ;
}
// Otherwise, print No
else {
cout << "No" ;
}
} // Driver Code int main()
{ int arr[] = { 29, 6, 4, 10 };
int N = sizeof (arr) / sizeof (arr[0]);
findSubset(arr, N);
return 0;
} |
// Java program for the above approach import java.io.*;
import java.lang.*;
import java.util.*;
class GFG
{ // Function to return gcd of a and b
static int gcd( int a, int b)
{
// Base Case
if (a == 0 )
return b;
// Find the GCD recursively
return gcd(b % a, a);
}
// Function to calculate the GCD
// of the array arr[]
static int findGCDofArray( int arr[], int N)
{
// Stores the GCD of array
int g = 0 ;
// Traverse the array arr[]
for ( int i = 0 ; i < N; i++)
{
// Update gcd of the array
g = gcd(g, arr[i]);
// If gcd is 1, then return 1
if (g == 1 ) {
return 1 ;
}
}
// Return the resultant GCD
return g;
}
// Function to check if a subset satisfying
// the given condition exists or not
static void findSubset( int arr[], int N)
{
// Calculate the gcd of the array
int gcd = findGCDofArray(arr, N);
// If gcd is 1, then print Yes
if (gcd == 1 ) {
System.out.println( "Yes" );
}
// Otherwise, print No
else {
System.out.println( "No" );
}
}
// Driver code
public static void main(String[] args)
{
// Given array
int arr[] = { 29 , 6 , 4 , 10 };
// length of the array
int N = arr.length;
// function call
findSubset(arr, N);
}
} // This code is contributed by Kingash. |
# Python3 program for the above approach # Function to return gcd of a and b def gcd(a, b):
# Base Case
if (a = = 0 ):
return b
# Find the GCD recursively
return gcd(b % a, a)
# Function to calculate the GCD # of the array arr[] def findGCDofArray(arr, N):
# Stores the GCD of array
g = 0
# Traverse the array arr[]
for i in range (N):
# Update gcd of the array
g = gcd(g, arr[i])
# If gcd is 1, then return 1
if (g = = 1 ):
return 1
# Return the resultant GCD
return g
# Function to check if a subset satisfying # the given condition exists or not def findSubset(arr, N):
# Calculate the gcd of the array
gcd = findGCDofArray(arr, N)
# If gcd is 1, then print Yes
if (gcd = = 1 ):
print ( "Yes" )
# Otherwise, print No
else :
print ( "No" )
# Driver Code if __name__ = = '__main__' :
arr = [ 29 , 6 , 4 , 10 ]
N = len (arr)
findSubset(arr, N)
# This code is contributed by mohit kumar 29.
|
// C# program for the above approach using System;
class GFG
{ // Function to return gcd of a and b
static int gcd( int a, int b)
{
// Base Case
if (a == 0)
return b;
// Find the GCD recursively
return gcd(b % a, a);
}
// Function to calculate the GCD
// of the array arr[]
static int findGCDofArray( int [] arr, int N)
{
// Stores the GCD of array
int g = 0;
// Traverse the array arr[]
for ( int i = 0; i < N; i++) {
// Update gcd of the array
g = gcd(g, arr[i]);
// If gcd is 1, then return 1
if (g == 1) {
return 1;
}
}
// Return the resultant GCD
return g;
}
// Function to check if a subset satisfying
// the given condition exists or not
static void findSubset( int [] arr, int N)
{
// Calculate the gcd of the array
int gcd = findGCDofArray(arr, N);
// If gcd is 1, then print Yes
if (gcd == 1) {
Console.Write( "Yes" );
}
// Otherwise, print No
else {
Console.Write( "No" );
}
}
// Driver code
public static void Main(String[] args)
{
int [] arr = { 29, 6, 4, 10 };
int N = arr.Length;
findSubset(arr, N);
}
} // This code is contributed by shivani |
<script> // javascript program for the above approach // Function to return gcd of a and b
function gcd(a , b) {
// Base Case
if (a == 0)
return b;
// Find the GCD recursively
return gcd(b % a, a);
}
// Function to calculate the GCD
// of the array arr
function findGCDofArray(arr , N) {
// Stores the GCD of array
var g = 0;
// Traverse the array arr
for (i = 0; i < N; i++) {
// Update gcd of the array
g = gcd(g, arr[i]);
// If gcd is 1, then return 1
if (g == 1) {
return 1;
}
}
// Return the resultant GCD
return g;
}
// Function to check if a subset satisfying
// the given condition exists or not
function findSubset(arr , N) {
// Calculate the gcd of the array
var gcd = findGCDofArray(arr, N);
// If gcd is 1, then print Yes
if (gcd == 1) {
document.write( "Yes" );
}
// Otherwise, print No
else {
document.write( "No" );
}
}
// Driver code
// Given array
var arr = [ 29, 6, 4, 10 ];
// length of the array
var N = arr.length;
// function call
findSubset(arr, N);
// This code contributed by gauravrajput1 </script> |
Yes
Time Complexity: O(N * log(M)), where M is the smallest element of the array.
Auxiliary Space: O(1)