Maximum possible sum less than or equal to k in a 2D array using each row

shubhamr

Given : A two dimensional array , values K and M

Problem : Find the maximum possible sum less than or equal K using all the rows (i.e there should be an element form each row) using exactly M elements.

This is a snippet of a program, I am having trouble implementing the conditions for each row and M.

for (int i = 0 ; i<n ; i++)
    for (int s=0; s<M; s++)
        for (int j=K;j>=0;j--)
            if (dp[s][j] && A[i] + j < K)
                dp[s + 1][j + A[i]] = true;

EDIT 1: Rows = M , i.e one element from each row has to be selected.

EDIT 2 : Dynamic Programming Solution, Thanks to @6502

ill ret(V(ill) col[101],ill prec[][101],ill s,ill row,ill m,ill k)
{
    if(prec[s][row])
    return prec[s][row];
    else
    {
        if(row==m+1)
        return s;
        ill best=-1;
        int j=row;

        for(int i=0;i<col[j].size();i++)
        {
            if(s+col[j][i] <= k)
            {
                ill x = ret (col,prec,s+col[j][i],row+1,m,k);
                if ((best==-1)||(x>best))
                best=x;

            }
        }
        prec[s][row]=best;
        return best;
    }


}
6502

The problem can be solved using dynamic programming by choosing as state the pair (s, row) where s is the current sum and row is the next row we need to include.

The maximal principle is valid because no matter on which choices we made in previous rows the result depends only on the current sum and the current row index.

In code (Python)

cache = {}

data = [[2, 3, 4],
        [2, 3, 4],
        [2, 3, 4]]

M = 3
K = 10

def msum(s, row):
    try:
        return cache[s, row]
    except KeyError:
        if row == M:
            return s
        best = None
        for v in data[row]:
            if s+v <= K:
                x = msum(s+v, row+1)
                if best is None or x > best:
                    best = x
        cache[s, row] = best
        return best

print msum(0, 0)

The function returns None if no solution exists (i.e. if even taking the smallest value from each row we end up exceeding K).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

maximum no. of elements in an array having sum less than or equal to k

From Dev

maximum no. of elements in an array having sum less than or equal to k

From Dev

greatest sum sub array such that each element is less than or equal to X

From Dev

Divide an array into k or less subparts to minimize the maximum sum of each part

From Dev

Divide an array into k or less subparts to minimize the maximum sum of each part

From Dev

Return all possible combinations of numbers in an array whose sum is less than or equal to n

From Dev

Get the sum of each individual row and column in a 2D array

From Dev

Sum of each individual row in a 2D Array

From Dev

How to return boolean statement if the sum of an arraylist is less than or equal to a maximum number?

From Dev

2D array symmetric to the main diagonal with row's sum equal to 1

From Dev

2D array symmetric to the main diagonal with row's sum equal to 1

From Dev

Find cumulative sums of each grouping in a row and then set the grouping equal to the maximum sum

From Dev

Given a sorted array and a parameter k, find the count of sum of two numbers greater than or equal to k

From Dev

Count number of times two elements in array are less than or equal to a sum value - Javascript

From Dev

sum's sum of divizors of numbers less than or equal to N

From Dev

Python - Sum a column where each row is less than two other columns

From Java

Perform sum over different slice of each row for 2D array

From Dev

compute sum up each 2 rows and replace them with another value if the sum is less than a specific value

From Dev

Finding the number of elements less than or equal to k in a multiset

From Dev

how to find items in list that their sum is less than or equal a number

From Dev

Is it possible to check if any of 2 sets of 3 ints is equal with less than 9 comparisons?

From Dev

How to find numbers in an array that are greater than, less than, or equal to a value?

From Dev

Maximum sum in a contiguous subarray of a given 2D array

From Dev

Finding the maximum row for each set of rows in 3D array

From Dev

count each character for each word in a string and put the word with less than 5 characters in array using php

From Dev

count each character for each word in a string and put the word with less than 5 characters in array using php

From Dev

How to check in 2d array if length of row and column are equal?

From Dev

Equal to or less than not working

From Dev

Less than or equal to;

Related Related

  1. 1

    maximum no. of elements in an array having sum less than or equal to k

  2. 2

    maximum no. of elements in an array having sum less than or equal to k

  3. 3

    greatest sum sub array such that each element is less than or equal to X

  4. 4

    Divide an array into k or less subparts to minimize the maximum sum of each part

  5. 5

    Divide an array into k or less subparts to minimize the maximum sum of each part

  6. 6

    Return all possible combinations of numbers in an array whose sum is less than or equal to n

  7. 7

    Get the sum of each individual row and column in a 2D array

  8. 8

    Sum of each individual row in a 2D Array

  9. 9

    How to return boolean statement if the sum of an arraylist is less than or equal to a maximum number?

  10. 10

    2D array symmetric to the main diagonal with row's sum equal to 1

  11. 11

    2D array symmetric to the main diagonal with row's sum equal to 1

  12. 12

    Find cumulative sums of each grouping in a row and then set the grouping equal to the maximum sum

  13. 13

    Given a sorted array and a parameter k, find the count of sum of two numbers greater than or equal to k

  14. 14

    Count number of times two elements in array are less than or equal to a sum value - Javascript

  15. 15

    sum's sum of divizors of numbers less than or equal to N

  16. 16

    Python - Sum a column where each row is less than two other columns

  17. 17

    Perform sum over different slice of each row for 2D array

  18. 18

    compute sum up each 2 rows and replace them with another value if the sum is less than a specific value

  19. 19

    Finding the number of elements less than or equal to k in a multiset

  20. 20

    how to find items in list that their sum is less than or equal a number

  21. 21

    Is it possible to check if any of 2 sets of 3 ints is equal with less than 9 comparisons?

  22. 22

    How to find numbers in an array that are greater than, less than, or equal to a value?

  23. 23

    Maximum sum in a contiguous subarray of a given 2D array

  24. 24

    Finding the maximum row for each set of rows in 3D array

  25. 25

    count each character for each word in a string and put the word with less than 5 characters in array using php

  26. 26

    count each character for each word in a string and put the word with less than 5 characters in array using php

  27. 27

    How to check in 2d array if length of row and column are equal?

  28. 28

    Equal to or less than not working

  29. 29

    Less than or equal to;

HotTag

Archive