Check if array sum can be made K by three operations on it
Given an array A of N integers and a positive integer K. Only three operations can be performed on this array:
- Replace an integer with the negative value of the integer,
- Add index number (1-based indexing) of the element to the element itself and
- Subtract index number of the element from the element itself.
The task is to check if the given array can be transformed, using any of above three allowed operations performed only once on each element. such that the sum of the array becomes K.
Examples:
Input : N = 3, K = 2
A[] = { 1, 1, 1 }
Output : Yes
Explanation
Replace index 0 element with -1. It will sum of array equal to k = 2.
Input : N = 4, K = 5
A[] = { 1, 2, 3, 4 }
Output : Yes
Pre Requisites Dynamic Programming
Approach: The idea is to use dynamic programming to solve the problem.
Declare a 2D Boolean array, dp[][], where dp[i][j] states if there is any way to obtain the sum of the array equal to j using some operations on the first i elements of the array.
dp[i][j] will be true if the sum is possible else it will be False.
Also, it is possible that the intermediate sum of the array is negative, in that case, do not perform any operation and ignore it thus letting the sum be always positive because k is always positive.
For calculating dp[i][j] we need the values of all states that can make a sum j if we apply an operation on a[i] and add it to the sum.
Below is the implementation of this approach
C++
#include <bits/stdc++.h>
using namespace std;
#define MAX 100
int check( int i, int sum, int n, int k, int a[],
int dp[MAX][MAX])
{
if (sum <= 0)
return false ;
if (i >= n) {
if (sum == k)
return true ;
return false ;
}
if (dp[i][sum] != -1)
return dp[i][sum];
dp[i][sum] = check(i + 1, sum - 2 * a[i], n,
k, a, dp) || check(i + 1, sum, n, k, a, dp);
dp[i][sum] = check(i + 1, sum - (i + 1), n,
k, a, dp) || dp[i][sum];
dp[i][sum] = check(i + 1, sum + i + 1, n,
k, a, dp) || dp[i][sum];
return dp[i][sum];
}
bool wrapper( int n, int k, int a[])
{
int sum = 0;
for ( int i = 0; i < n; i++)
sum += a[i];
int dp[MAX][MAX];
memset (dp, -1, sizeof (dp));
return check(0, sum, n, k, a, dp);
}
int main()
{
int a[] = { 1, 2, 3, 4 };
int n = 4, k = 5;
(wrapper(n, k, a) ? (cout << "Yes" ) : (cout << "No" ));
return 0;
}
|
Java
class GFG
{
static int MAX = 100 ;
static int check( int i, int sum, int n,
int k, int a[], int dp[][])
{
if (sum <= 0 )
{
return 0 ;
}
if (i >= n)
{
if (sum == k)
{
return 1 ;
}
return 0 ;
}
if (dp[i][sum] != - 1 )
{
return dp[i][sum];
}
dp[i][sum] = check(i + 1 , sum - 2 * a[i], n, k, a, dp) |
check(i + 1 , sum, n, k, a, dp);
dp[i][sum] = check(i + 1 , sum - (i + 1 ), n,
k, a, dp) | dp[i][sum];
dp[i][sum] = check(i + 1 , sum + i + 1 , n,
k, a, dp) | dp[i][sum];
return dp[i][sum];
}
static int wrapper( int n, int k, int a[])
{
int sum = 0 ;
for ( int i = 0 ; i < n; i++)
{
sum += a[i];
}
int [][] dp = new int [MAX][MAX];
for ( int i = 0 ; i < MAX; i++)
{
for ( int j = 0 ; j < MAX; j++)
{
dp[i][j] = - 1 ;
}
}
return check( 0 , sum, n, k, a, dp);
}
public static void main(String[] args)
{
int a[] = { 1 , 2 , 3 , 4 };
int n = 4 , k = 5 ;
if (wrapper(n, k, a) == 1 ) {
System.out.println( "Yes" );
} else {
System.out.println( "No" );
}
}
}
|
C#
using System;
class GFG
{
static int MAX = 100;
static int check( int i, int sum, int n,
int k, int []a, int [,]dp)
{
if (sum <= 0)
{
return 0;
}
if (i >= n)
{
if (sum == k)
{
return 1;
}
return 0;
}
if (dp[i, sum] != -1)
{
return dp[i, sum];
}
dp[i,sum] = check(i + 1, sum - 2 * a[i], n, k, a, dp) |
check(i + 1, sum, n, k, a, dp);
dp[i,sum] = check(i + 1, sum - (i + 1), n,
k, a, dp) | dp[i,sum];
dp[i,sum] = check(i + 1, sum + i + 1, n,
k, a, dp) | dp[i,sum];
return dp[i, sum];
}
static int wrapper( int n, int k, int []a)
{
int sum = 0;
for ( int i = 0; i < n; i++)
{
sum += a[i];
}
int [,] dp = new int [MAX,MAX];
for ( int i = 0; i < MAX; i++)
{
for ( int j = 0; j < MAX; j++)
{
dp[i, j] = -1;
}
}
return check(0, sum, n, k, a, dp);
}
static public void Main ()
{
int []a = {1, 2, 3, 4};
int n = 4, k = 5;
if (wrapper(n, k, a) == 1)
{
Console.WriteLine( "Yes" );
}
else
{
Console.WriteLine( "No" );
}
}
}
|
Python3
MAX = 100
def check(i, add, n, k, a, dp):
if add < = 0 :
return False
if i > = n:
if add = = k:
return True
return False
if dp[i][add] ! = - 1 :
return dp[i][add]
dp[i][add] = (check(i + 1 , add - 2 * a[i], n,
k, a, dp) or check(i + 1 , add, n, k, a, dp))
dp[i][add] = (check(i + 1 , add - (i + 1 ), n,
k, a, dp) or dp[i][add])
dp[i][add] = (check(i + 1 , add + i + 1 , n,
k, a, dp) or dp[i][add])
return dp[i][add]
def wrapper(n, k, a):
add = 0
for i in range (n):
add + = a[i]
dp = [ - 1 ] * MAX
for i in range ( MAX ):
dp[i] = [ - 1 ] * MAX
return check( 0 , add, n, k, a, dp)
if __name__ = = "__main__" :
a = [ 1 , 2 , 3 , 4 ]
n = 4
k = 5
print ( "Yes" ) if wrapper(n, k, a) else print ( "No" )
|
Javascript
<script>
var MAX = 100;
function check(i, sum, n, k, a, dp)
{
if (sum <= 0)
return false ;
if (i >= n) {
if (sum == k)
return true ;
return false ;
}
if (dp[i][sum] != -1)
return dp[i][sum];
dp[i][sum] = check(i + 1, sum - 2 * a[i], n,
k, a, dp) || check(i + 1, sum, n, k, a, dp);
dp[i][sum] = check(i + 1, sum - (i + 1), n,
k, a, dp) || dp[i][sum];
dp[i][sum] = check(i + 1, sum + i + 1, n,
k, a, dp) || dp[i][sum];
return dp[i][sum];
}
function wrapper(n, k, a)
{
var sum = 0;
for ( var i = 0; i < n; i++)
sum += a[i];
var dp = Array.from(Array(MAX), ()=>
Array(MAX).fill(-1));
return check(0, sum, n, k, a, dp);
}
var a = [ 1, 2, 3, 4 ];
var n = 4, k = 5;
(wrapper(n, k, a) ? (document.write( "Yes" )) :
(document.write( "No" )));
</script>
|
Complexity Analysis:
- Time Complexity: O(N*MAX), where N is the number of elements in array and MAX is the defined constant .
- Auxiliary Space: O(MAX*MAX) , where MAX is constant define 100.
Last Updated :
22 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...