d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Need Help With Algorithms And Data Structure Probl > N X N Problem
Add Reply New Topic New Poll
Member
Posts: 3,397
Joined: Oct 25 2012
Gold: 3,575.00
Sep 7 2017 12:13pm
I have an issue.

I need a solution to my problem where I need an algorithm to find the maximum sum in an N x N with a method faster than N^6

The N x N contains both positive and negative numbers.

Something like this:



I thought maybe a divide-and-conquer method could be used, but I'm not sure exactly how that would work.

Any feedback is helpful!

Member
Posts: 467
Joined: Nov 18 2013
Gold: 0.00
Sep 7 2017 04:55pm
are you looking for the sum of all the values in the matrix?
Member
Posts: 3,397
Joined: Oct 25 2012
Gold: 3,575.00
Sep 7 2017 04:59pm
Quote (Arstar04 @ 8 Sep 2017 01:55)
are you looking for the sum of all the values in the matrix?


It's any given N x N matrix with negative and positive numbers.

I need an algorithm that can find the box with the biggest sum, it can be from 1 x 1 to N x N.

The box must be the biggest possible sum of any box in the matrix.
Member
Posts: 467
Joined: Nov 18 2013
Gold: 0.00
Sep 7 2017 04:59pm
Quote (Wallesmegusta @ Sep 7 2017 05:59pm)
It's any given N x N matrix with negative and positive numbers.

I need an algorithm that can find the box with the biggest sum, it can be from 1 x 1 to N x N.

The box must be the biggest possible sum of any box in the matrix.


so in this case, 100 is the biggest value?
Member
Posts: 3,397
Joined: Oct 25 2012
Gold: 3,575.00
Sep 7 2017 05:04pm
Quote (Arstar04 @ 8 Sep 2017 01:59)
so in this case, 100 is the biggest value?


no, it's much bigger. You need to put together all the numbers in the box, like the grey box gives a sum of 142.
Member
Posts: 467
Joined: Nov 18 2013
Gold: 0.00
Sep 7 2017 05:06pm
Quote (Wallesmegusta @ Sep 7 2017 06:04pm)
no, it's much bigger. You need to put together all the numbers in the box, like the grey box gives a sum of 142.


so if you just iterate through the matrix you would just do:

int sum = 0;
for(int row = 0; row < matrix.length; row ++){
for(int col = 0; col < matrix[row].length; col++){
sum += matrix[row][col];
}
}

return sum;

this would add all the values in the matrix together.
Member
Posts: 3,397
Joined: Oct 25 2012
Gold: 3,575.00
Sep 7 2017 05:11pm
Quote (Arstar04 @ 8 Sep 2017 02:06)
so if you just iterate through the matrix you would just do:

int sum = 0;
for(int row = 0; row < matrix.length; row ++){
for(int col = 0; col < matrix[row].length; col++){
sum += matrix[row][col];
}
}

return sum;

this would add all the values in the matrix together.


Ok, but that is not necessarily the biggest sum in the matrix, there are negative numbers aswell, so I need to sort those out aswell.
Member
Posts: 467
Joined: Nov 18 2013
Gold: 0.00
Sep 7 2017 05:13pm
ahhh i didnt understand what you were asking. https://stackoverflow.com/questions/2643908/getting-the-submatrix-with-maximum-sum this seems to be what youre looking for
Member
Posts: 3,397
Joined: Oct 25 2012
Gold: 3,575.00
Sep 7 2017 05:16pm
Quote (Arstar04 @ 8 Sep 2017 02:13)
ahhh i didnt understand what you were asking. https://stackoverflow.com/questions/2643908/getting-the-submatrix-with-maximum-sum this seems to be what youre looking for


Yes, that's it :) thanks.
Member
Posts: 467
Joined: Nov 18 2013
Gold: 0.00
Sep 7 2017 05:18pm
Quote (Wallesmegusta @ Sep 7 2017 06:16pm)
Yes, that's it :) thanks.


http://www.geeksforgeeks.org/dynamic-programming-set-27-max-sum-rectangle-in-a-2d-matrix/

this could help too!
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll