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:
1) Replace an integer with the negative value of the integer,
2) Add index number (1-based indexing) of the element to the element itself and
3) 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

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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 firsti 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++

 /* C++ Program to find if Array can have a sum    of K by applying three types of possible     operations on it */ #include using namespace std; #define MAX 100    // Check if it is possible to achieve a sum with // three operation allowed. int check(int i, int sum, int n, int k, int a[],                                int dp[MAX][MAX]) {     // If sum is negative.     if (sum <= 0)         return false;        // If going out of bound.     if (i >= n) {         // If sum is achieved.         if (sum == k)             return true;            return false;     }        // If the current state is not evaluated yet.     if (dp[i][sum] != -1)         return dp[i][sum];        // Replacing element with negative value of     // the element.     dp[i][sum] = check(i + 1, sum - 2 * a[i], n,            k, a, dp) || check(i + 1, sum, n, k, a, dp);        // Substracting index number from the element.     dp[i][sum] = check(i + 1, sum - (i + 1), n,           k, a, dp) || dp[i][sum];        // Adding index number to the element.     dp[i][sum] = check(i + 1, sum + i + 1, n,                       k, a, dp) || dp[i][sum];        return dp[i][sum]; }    // Wrapper Function 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); }    // Driver Code int main() {     int a[] = { 1, 2, 3, 4 };     int n = 4, k = 5;     (wrapper(n, k, a) ? (cout << "Yes") : (cout << "No"));     return 0; }

Java

 /* Java Program to find if Array can have a sum of K by applying three types of possible  operations on it */ class GFG  {        static int MAX = 100;        // Check if it is possible to achieve a sum with     // three operation allowed.     static int check(int i, int sum, int n,                      int k, int a[], int dp[][])     {         // If sum is negative.         if (sum <= 0)          {             return 0;         }            // If going out of bound.         if (i >= n)         {             // If sum is achieved.             if (sum == k)             {                 return 1;             }                return 0;         }            // If the current state is not evaluated yet.         if (dp[i][sum] != -1)         {             return dp[i][sum];         }            // Replacing element with negative value of         // the element.         dp[i][sum] = check(i + 1, sum - 2 * a[i], n, k, a, dp) |                                 check(i + 1, sum, n, k, a, dp);            // Substracting index number from the element.         dp[i][sum] = check(i + 1, sum - (i + 1), n,                 k, a, dp) | dp[i][sum];            // Adding index number to the element.         dp[i][sum] = check(i + 1, sum + i + 1, n,                 k, a, dp) | dp[i][sum];            return dp[i][sum];     }        // Wrapper Function     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);     }        // Driver Code     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");         }     } }    // This code is contributed by Princi Singh

C#

 // C# Program to find if Array can have a sum // of K by applying three types of possible     using System;    class GFG {     static int MAX = 100;        // Check if it is possible to achieve a sum with     // three operation allowed.     static int check(int i, int sum, int n,                      int k, int []a, int [,]dp)     {         // If sum is negative.         if (sum <= 0)          {             return 0;         }            // If going out of bound.         if (i >= n)         {             // If sum is achieved.             if (sum == k)             {                 return 1;             }                return 0;         }            // If the current state is not evaluated yet.         if (dp[i, sum] != -1)         {             return dp[i, sum];         }            // Replacing element with negative value of         // the element.         dp[i,sum] = check(i + 1, sum - 2 * a[i], n, k, a, dp) |                                 check(i + 1, sum, n, k, a, dp);            // Substracting index number from the element.         dp[i,sum] = check(i + 1, sum - (i + 1), n,                 k, a, dp) | dp[i,sum];            // Adding index number to the element.         dp[i,sum] = check(i + 1, sum + i + 1, n,                 k, a, dp) | dp[i,sum];            return dp[i, sum];     }        // Wrapper Function     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);     }        // Driver Code     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");         }     } }    // This code is contributed by ajit_0023

Python3

Output:

Yes

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.