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++
// 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
// 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
# 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#
// 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
JavaScript
<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>
Time Complexity: O(N * log(M)), where M is the smallest element of the array.
Auxiliary Space: O(1)
Similar Reads
Minimize steps to reduce Array by deleting an element and all multiples in a step Given an array arr[] of size N, the task is to find the minimum number of operations to reduce the array by deleting an element and all its multiples from the array in one operation. Examples: Input: N = 5, arr[] = {5, 2, 3, 8, 7}Output: 4Explanation: Removing 2, will make it remove 8 (as it is a mu
9 min read
Minimum steps to make sum and the product of all elements of array non-zero Given an array arr of N integers, the task is to find the minimum steps in which the sum and product of all elements of the array can be made non-zero. In one step any element of the array can be incremented by 1.Examples: Input: N = 4, arr[] = {0, 1, 2, 3} Output: 1 Explanation: As product of all e
7 min read
Smallest Value that cannot be represented as sum of any subset of a given array Given an array of positive numbers, the task is to find the smallest positive integer value that cannot be represented as the sum of elements of any subset of a given set. Examples: Input: arr[] = {1, 10, 3, 11, 6, 15};Output: 2Input: arr[] = {1, 1, 1, 1};Output: 5Input: arr[] = {1, 1, 3, 4};Output:
10 min read
Minimum sum of values subtracted from array elements to make all array elements equal Given an array arr[] consisting of N positive integers, the task is to find the sum of all the array elements required to be subtracted from each array element such that remaining array elements are all equal. Examples: Input: arr[] = {1, 2}Output: 1Explanation: Subtracting 1 from arr[1] modifies ar
4 min read
Modify array to another given array by replacing array elements with the sum of the array | Set-2 Given an Array input[] consisting only of 1s initially and an array target[] of size N, the task is to check if the array input[] can be converted to target[] by replacing input[i] with the sum of array elements in each step. Examples: Input: input[] = { 1, 1, 1 }, target[] = { 9, 3, 5 } Output: YES
10 min read