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
Approach: This problem can be solved using Dynamic Programming.
- 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.
- So, we will iterate the array from i = [0, N], k = [0, K] and check if the current sum(k) exist or not.
- 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 <bits/stdc++.h> using namespace std; // Function that prints whether is it // possible to make sum equal to K void PossibleSum(int n, int m, vector<vector<int> > v, int k) { int dp[n + 1][k + 1] = { 0 }; // 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) cout << "YES\n"; else cout << "NO\n"; } // Driver Code int main() { int N = 2, M = 10, K = 5; vector<vector<int> > 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 = 2M = 10K = 5arr = [ [ 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 |
YES
Time Complexity: O(N * M * K)
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.
Recommended Posts:
- Minimum element of each row and each column in a matrix
- Program to find the Sum of each Row and each Column of a Matrix
- Print all possible paths from the first row to the last row in a 2D array
- Count ways to reach Nth Stairs by taking 1 and 2 steps with exactly one 3 step
- Sum of matrix in which each element is absolute difference of its row and column numbers
- Sum of matrix element where each elements is integer division of row and column
- Maximum path sum that starting with any cell of 0-th row and ending with any cell of (N-1)-th row
- Sum of GCDs of each row of the given matrix
- Find maximum element of each row in a matrix
- Maximise array sum after taking non-overlapping sub-arrays of length K
- Maximum subsequence sum possible by multiplying each element by its index
- Check if it is possible to create a matrix such that every row has A 1s and every column has B 1s
- Maximum sum of elements from each row in the matrix
- Maximum sum of a Matrix where each value is from a unique row and column
- Maximum possible GCD after replacing at most one element in the given array
- Number of ways to reach Nth floor by taking at-most K leaps
- Program to multiply two Matrix by taking data from user
- Check if it is possible to get back to 12'0 clock only by adding or subtracting given seconds
- Check if a given matrix can be converted to another given matrix by row and column exchanges
- Maximum sum in an array such that every element has exactly one adjacent element to it
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.

