Related Articles

# Check if it is possible to get given sum by taking one element from each row

• Last Updated : 07 Jun, 2021

Given a 2-D array of N rows and M columns and an integer K. The task is to find whether is it possible to take exactly one element from each row of the given array and make a total sum equal to K.
Examples:

```Input: N = 2, M = 10, K = 5
arr = {{4, 0, 15, 3, 2, 20, 10, 1, 5, 4},
{4, 0, 10, 3, 2, 25, 4, 1, 5, 4}}
Output: YES
Explanation:
Take 2 from first row and 3 from second row.
2 + 3 = 5
So, we can make 5 by taking exactly one element
from each row.

Input:N = 3, M = 5, K = 5
arr = {{4, 3, 4, 5, 4},
{2, 2, 3, 4, 3},
{2, 1, 3, 3, 2}}
Output: NO```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Approach: This problem can be solved using Dynamic Programming

1. We can make a 2-D binary array DP[][] of N rows and K columns. where DP[i][j] = 1 represents that we can make a sum equal to j by taking exactly one element from each row till i.

2. So, we will iterate the array from i = [0, N], k = [0, K] and check if the current sum(k) exist or not.

3. If the current sum exist then we will iterate over the column and update the array for every possible sum which is less than or equal to K.

Below is the implementation of the above approach

## C++

 `// C++ implementation to find``// whether is it possible to``// make sum equal to K``#include ``using` `namespace` `std;` `// Function that prints whether is it``// possible to make sum equal to K``void` `PossibleSum(``int` `n, ``int` `m,``              ``vector > v,``              ``int` `k)``{``    ``int` `dp[n + 1][k + 1] = { 0 };` `    ``// Base case``    ``dp = 1;` `    ``for` `(``int` `i = 0; i < n; i++)``    ``{``        ``for` `(``int` `j = 0; j <= k; j++)``        ``{``            ``// Condition if we can make``            ``// sum equal to current column``            ``// by using above rows``            ``if` `(dp[i][j] == 1)``            ``{``                ``// Iterate through current``                ``// column and check whether``                ``// we can make sum less than``                ``// or equal to k``                ``for` `(``int` `d = 0; d < m; d++)``                ``{``                    ``if` `((j + v[i][d]) <= k)``                    ``{``                        ``dp[i + 1][j + v[i][d]] = 1;``                    ``}``                ``}``            ``}``        ``}``    ``}` `    ``// Printing whether is it``    ``// possible or not``    ``if` `(dp[n][k] == 1)``        ``cout << ``"YES\n"``;``    ``else``        ``cout << ``"NO\n"``;``}` `// Driver Code``int` `main()``{``    ``int` `N = 2, M = 10, K = 5;` `    ``vector > arr = { { 4, 0, 15, 3, 2,``                                  ``20, 10, 1, 5, 4 },``                                 ``{ 4, 0, 10, 3, 2,``                                  ``25, 4, 1, 5, 4 } };` `    ``PossibleSum(N, M, arr, K);` `    ``return` `0;``}`

## Java

 `// Java implementation to find``// whether is it possible to``// make sum equal to K``import` `java.io.*;``import` `java.util.*;` `class` `GFG {``    ` `// Function that prints whether is it``// possible to make sum equal to K``static` `void` `PossibleSum(``int` `n, ``int` `m,``                        ``int``[][] v, ``int` `k)``{``    ``int``[][] dp = ``new` `int``[n + ``1``][k + ``1``];` `    ``// Base case``    ``dp[``0``][``0``] = ``1``;` `    ``for``(``int` `i = ``0``; i < n; i++)``    ``{``       ``for``(``int` `j = ``0``; j <= k; j++)``       ``{``           ` `          ``// Condition if we can make``          ``// sum equal to current column``          ``// by using above rows``          ``if` `(dp[i][j] == ``1``)``          ``{``              ` `              ``// Iterate through current``              ``// column and check whether``              ``// we can make sum less than``              ``// or equal to k``              ``for``(``int` `d = ``0``; d < m; d++)``              ``{``                 ``if` `((j + v[i][d]) <= k)``                 ``{``                     ``dp[i + ``1``][j + v[i][d]] = ``1``;``                 ``}``              ``}``          ``}``       ``}``    ``}``    ` `    ``// Printing whether is it``    ``// possible or not``    ``if` `(dp[n][k] == ``1``)``        ``System.out.println(``"YES"``);``    ``else``        ``System.out.println(``"NO"``);``}` `// Driver code``public` `static` `void` `main(String[] args)``{``    ``int` `N = ``2``, M = ``10``, K = ``5``;``    ``int``[][] arr = ``new` `int``[][]{ { ``4``, ``0``, ``15``, ``3``, ``2``,``                                ``20``, ``10``, ``1``, ``5``, ``4` `},``                               ``{ ``4``, ``0``, ``10``, ``3``, ``2``,``                                ``25``, ``4``, ``1``, ``5``, ``4` `} };``    ``PossibleSum(N, M, arr, K);``}``}` `// This code is contributed by coder001`

## Python3

 `# Python3 implementation to find``# whether is it possible to``# make sum equal to K` `# Function that prints whether is it``# possible to make sum equal to K``def` `PossibleSum(n, m, v, k):``    ` `    ``dp ``=` `[[``0``] ``*` `(k ``+` `1``) ``for` `i ``in` `range``(n ``+` `1``)]``    ` `    ``# Base case``    ``dp[``0``][``0``] ``=` `1` `    ``for` `i ``in` `range``(n):``        ``for` `j ``in` `range``(k ``+` `1``):` `            ``# Condition if we can make``            ``# sum equal to current column``            ``# by using above rows``            ``if` `dp[i][j] ``=``=` `1``:` `                ``# Iterate through current``                ``# column and check whether``                ``# we can make sum less than``                ``# or equal to k``                ``for` `d ``in` `range``(m):``                    ``if` `(j ``+` `v[i][d]) <``=` `k:``                        ``dp[i ``+` `1``][j ``+` `v[i][d]] ``=` `1` `    ``# Printing whether is it``    ``# possible or not``    ``if` `dp[n][k] ``=``=` `1``:``        ``print``(``"YES"``)``    ``else``:``        ``print``(``"NO"``)` `# Driver Code``N ``=` `2``M ``=` `10``K ``=` `5``arr ``=` `[ [ ``4``, ``0``, ``15``, ``3``, ``2``,``         ``20``, ``10``, ``1``, ``5``, ``4` `],``        ``[ ``4``, ``0``, ``10``, ``3``, ``2``,``          ``25``, ``4``, ``1``, ``5``, ``4` `] ]` `PossibleSum(N, M, arr, K)` `# This code is contributed by divyamohan123`

## C#

 `// C# implementation to find``// whether is it possible to``// make sum equal to K``using` `System;``class` `GFG{``    ` `// Function that prints whether is it``// possible to make sum equal to K``static` `void` `PossibleSum(``int` `n, ``int` `m,``                        ``int``[,] v, ``int` `k)``{``    ``int``[,] dp = ``new` `int``[n + 1, k + 1];` `    ``// Base case``    ``dp[0, 0] = 1;` `    ``for``(``int` `i = 0; i < n; i++)``    ``{``        ``for``(``int` `j = 0; j <= k; j++)``        ``{``                ` `            ``// Condition if we can make``            ``// sum equal to current column``            ``// by using above rows``            ``if` `(dp[i, j] == 1)``            ``{``                    ` `                ``// Iterate through current``                ``// column and check whether``                ``// we can make sum less than``                ``// or equal to k``                ``for``(``int` `d = 0; d < m; d++)``                ``{``                    ``if` `((j + v[i, d]) <= k)``                    ``{``                        ``dp[i + 1, j + v[i, d]] = 1;``                    ``}``                ``}``            ``}``        ``}``    ``}``    ` `    ``// Printing whether is it``    ``// possible or not``    ``if` `(dp[n, k] == 1)``        ``Console.WriteLine(``"YES"``);``    ``else``        ``Console.WriteLine(``"NO"``);``}` `// Driver code``public` `static` `void` `Main(String[] args)``{``    ``int` `N = 2, M = 10, K = 5;``    ``int``[,] arr = ``new` `int``[,]{ { 4, 0, 15, 3, 2,``                              ``20, 10, 1, 5, 4 },``                             ``{ 4, 0, 10, 3, 2,``                              ``25, 4, 1, 5, 4 } };``    ``PossibleSum(N, M, arr, K);``}``}` `// This code is contributed by 29AjayKumar`

## Javascript

 ``
Output:
`YES`

Time Complexity: O(N * M * K)

My Personal Notes arrow_drop_up