Reduce sum of any subset of an array to 1 by multiplying all its elements by any value
Last Updated :
10 Feb, 2022
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++
#include <bits/stdc++.h>
using namespace std;
int gcd( int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
int findGCDofArray( int arr[], int N)
{
int g = 0;
for ( int i = 0; i < N; i++) {
g = gcd(g, arr[i]);
if (g == 1) {
return 1;
}
}
return g;
}
void findSubset( int arr[], int N)
{
int gcd = findGCDofArray(arr, N);
if (gcd == 1) {
cout << "Yes" ;
}
else {
cout << "No" ;
}
}
int main()
{
int arr[] = { 29, 6, 4, 10 };
int N = sizeof (arr) / sizeof (arr[0]);
findSubset(arr, N);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG
{
static int gcd( int a, int b)
{
if (a == 0 )
return b;
return gcd(b % a, a);
}
static int findGCDofArray( int arr[], int N)
{
int g = 0 ;
for ( int i = 0 ; i < N; i++)
{
g = gcd(g, arr[i]);
if (g == 1 ) {
return 1 ;
}
}
return g;
}
static void findSubset( int arr[], int N)
{
int gcd = findGCDofArray(arr, N);
if (gcd == 1 ) {
System.out.println( "Yes" );
}
else {
System.out.println( "No" );
}
}
public static void main(String[] args)
{
int arr[] = { 29 , 6 , 4 , 10 };
int N = arr.length;
findSubset(arr, N);
}
}
|
Python3
def gcd(a, b):
if (a = = 0 ):
return b
return gcd(b % a, a)
def findGCDofArray(arr, N):
g = 0
for i in range (N):
g = gcd(g, arr[i])
if (g = = 1 ):
return 1
return g
def findSubset(arr, N):
gcd = findGCDofArray(arr, N)
if (gcd = = 1 ):
print ( "Yes" )
else :
print ( "No" )
if __name__ = = '__main__' :
arr = [ 29 , 6 , 4 , 10 ]
N = len (arr)
findSubset(arr, N)
|
C#
using System;
class GFG
{
static int gcd( int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static int findGCDofArray( int [] arr, int N)
{
int g = 0;
for ( int i = 0; i < N; i++) {
g = gcd(g, arr[i]);
if (g == 1) {
return 1;
}
}
return g;
}
static void findSubset( int [] arr, int N)
{
int gcd = findGCDofArray(arr, N);
if (gcd == 1) {
Console.Write( "Yes" );
}
else {
Console.Write( "No" );
}
}
public static void Main(String[] args)
{
int [] arr = { 29, 6, 4, 10 };
int N = arr.Length;
findSubset(arr, N);
}
}
|
Javascript
<script>
function gcd(a , b) {
if (a == 0)
return b;
return gcd(b % a, a);
}
function findGCDofArray(arr , N) {
var g = 0;
for (i = 0; i < N; i++) {
g = gcd(g, arr[i]);
if (g == 1) {
return 1;
}
}
return g;
}
function findSubset(arr , N) {
var gcd = findGCDofArray(arr, N);
if (gcd == 1) {
document.write( "Yes" );
}
else {
document.write( "No" );
}
}
var arr = [ 29, 6, 4, 10 ];
var N = arr.length;
findSubset(arr, N);
</script>
|
Time Complexity: O(N * log(M)), where M is the smallest element of the array.
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...