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


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.

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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 

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Output:

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.




My Personal Notes arrow_drop_up

Interns at Geeksforgeeks

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.