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

GameOfChess

I have solved this problem but all the explanation given are with DP solutions having complexity at least N^3. I have solved it in N^2 I am wondering if my solution is wrong.

We are given a 2D array of integers (+ve and -ve), we are two find the subarray of this array having the maximum possible sum.

Algorithm that I have used is to fix 4 indices. For 2 columns I span from 0 to N to find where the maximum sum occurs and then I span from N to 0. From these two results I deduce from which column to which one will the sum be maximum. S Same approach I use for rows. Once, I get the 4 indices, I am done. Complexity: O(N^2)

Here is my code:

#include <iostream>

using namespace std;

template<size_t n>
int find_col_sum(int A[][n], int m, int r1, int r2, int c) {
    int sum = 0;
    for (int i = r1; i <= r2; i++)
        sum += A[i][c];
    return sum;
}
template<size_t n>
int find_row_sum(int A[][n], int m, int c1, int c2, int r) {
    int sum = 0;
    for (int i = c1; i <= c2; i++)
        sum += A[r][i];
    return sum;
}

template<size_t n>
void max_sum_array(int A[][n], int m, int &i1, int &i2, int &i3, int &i4) {
    int mx1 = 0, mx2 = 0;
    int sum = 0, max1_sum = 0;
    int max2_sum = 0;

    //0 to col
    for (int i = 0; i < n; i++) {
        sum += find_col_sum(A,m,0,n-1,i);
        if (sum > max1_sum) {
            mx1 = i;
            max1_sum = sum;
        }
    }
    //n-1 to col
    sum = 0; mx2 = n-1;
    for (int i = n-1; i >= 0; i--) {
        sum += find_col_sum(A,m,0,n-1,i);
        if (sum > max2_sum) {
            mx2 = i;
            max2_sum = sum;
        }
    }
    if (mx1 <= mx2) {
        if (max1_sum >= max2_sum) {
            mx2 = mx1; mx1 = 0;
        }
        else {
            mx1 = mx2; mx2 = n-1;
        }
    }
    else
        swap(mx1,mx2);
    i1 = mx1; i2 = mx2;
    mx1 = 0; mx2 = m-1;
    max1_sum = 0; max2_sum = 0;
    sum = 0;
    //0 to row
    for (int i = 0; i < m; i++) {
        sum += find_row_sum(A,m,i1,i2,i);
        if (sum > max1_sum) {
            mx1 = i;
            max1_sum = sum;
        }
    }
    sum = 0;
    //m-1 to row
    for (int i = m-1; i >= 0; i--) {
        sum += find_row_sum(A,m,i1,i2,i);
        if (sum > max2_sum) {
            mx2 = i;
            max2_sum = sum;
        }
    }
    if (mx1 <= mx2) {
        if (max1_sum >= max2_sum) {
            mx2 = mx1; mx1 = 0;
        }
        else {
            mx1 = mx2; mx2 = m-1;
        }
    } else
        swap(mx1,mx2);
    i3 = mx1; i4 = mx2;
}

int main() {
    int A[][4] = {{0,-2,-7,0},{9,2,-6,2},{-4,1-4,1},{-1,8,0,-2}};
    int i1 = 0, i2 = 0, i3 = 0, i4 = 0;

    max_sum_array(A,4,i1,i2,i3,i4);
    cout<<i1<<" "<<i2<<" "<<i3<<" "<<i4<<endl;

    return 0;
}
kraskevich

It's wrong. For instance, if the array is {{-1, -1, -1}, {-1, 1, -1},{-1, -1, -1}}, it prints 0 0 0 0, while the correct answer's 1 1 1 1 (it's optimal to take the middle element as it's the only positive one).

Here is a correct O(n^3) solution: let's fix an upper row. Let's create an array of zeros. Now we'll iterate over rows from this one to the last one. We'll add each row to the array and call a 1-D solution for this array. After checking all upper rows, we just need to print the largest answer.

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 Sum SubArray

From Dev

Finding a maximum sum contiguous sub array

From Dev

Find the largest sum subarray from the given array using segment trees

From Dev

Find numbers of subarray of an array whose sum is divided by given number

From Dev

Maximum subarray sum such that sum is odd

From Dev

Find path of FIXED length through a 2D array which gives maximum sum

From Dev

Finding path through 2D array with sum closest to given number

From Dev

Elegant way to find contiguous subarray within an array in JavaScript?

From Dev

From given row(x),column(y), find 3x3 subarray of a 2D 9x9 array

From Dev

Finding a maximum sum contiguous sub array - another version

From Dev

Find widest subarray with given sum (array slicing)

From Dev

Maximum subarray sum modulo M

From Dev

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

From Dev

largest sum of contiguous subarray No Larger than k

From Dev

Given 2d array of integers, find a path that sum up to a given number recursively

From Dev

Print SubArray of Maximum Contiguous product in Array

From Dev

Finding a maximum sum contiguous sub array

From Dev

Maximum subarray with given number of elements

From Dev

How to find the biggest sum of a given scope in a 2D array?

From Dev

Assigning memory for contiguous 2D array

From Dev

how to find the length of the longest contiguous subarray whose sum is divisible by a given number

From Dev

Maximum-sum subarray given constraints on indices

From Dev

Unable to understand algorithm to find maximum sum of Subarray

From Dev

How to find the maximum value along the topmost row and leftmost column of a subarray in a 2d array?

From Dev

find the maximum value of sum of subarray modulo M

From Dev

Finding subarray with given sum

From Dev

Maximum contiguous sum using Divide and Conquer

From Dev

Maximum subarray of size HxW within a 2D matrix

From Dev

Given a 2D matrix of size MxN with positive integer values, find the closed loop that has the maximum sum

Related Related

  1. 1

    Maximum Sum SubArray

  2. 2

    Finding a maximum sum contiguous sub array

  3. 3

    Find the largest sum subarray from the given array using segment trees

  4. 4

    Find numbers of subarray of an array whose sum is divided by given number

  5. 5

    Maximum subarray sum such that sum is odd

  6. 6

    Find path of FIXED length through a 2D array which gives maximum sum

  7. 7

    Finding path through 2D array with sum closest to given number

  8. 8

    Elegant way to find contiguous subarray within an array in JavaScript?

  9. 9

    From given row(x),column(y), find 3x3 subarray of a 2D 9x9 array

  10. 10

    Finding a maximum sum contiguous sub array - another version

  11. 11

    Find widest subarray with given sum (array slicing)

  12. 12

    Maximum subarray sum modulo M

  13. 13

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

  14. 14

    largest sum of contiguous subarray No Larger than k

  15. 15

    Given 2d array of integers, find a path that sum up to a given number recursively

  16. 16

    Print SubArray of Maximum Contiguous product in Array

  17. 17

    Finding a maximum sum contiguous sub array

  18. 18

    Maximum subarray with given number of elements

  19. 19

    How to find the biggest sum of a given scope in a 2D array?

  20. 20

    Assigning memory for contiguous 2D array

  21. 21

    how to find the length of the longest contiguous subarray whose sum is divisible by a given number

  22. 22

    Maximum-sum subarray given constraints on indices

  23. 23

    Unable to understand algorithm to find maximum sum of Subarray

  24. 24

    How to find the maximum value along the topmost row and leftmost column of a subarray in a 2d array?

  25. 25

    find the maximum value of sum of subarray modulo M

  26. 26

    Finding subarray with given sum

  27. 27

    Maximum contiguous sum using Divide and Conquer

  28. 28

    Maximum subarray of size HxW within a 2D matrix

  29. 29

    Given a 2D matrix of size MxN with positive integer values, find the closed loop that has the maximum sum

HotTag

Archive