Suppose we have a two-dimensional array A (n X n). All elements of A are either O or 1. We also have a given integer K. Our task is to find the number of all possible "rectangles" in A containing elements with the sum K.
To give an example , if A =
0 0 1 0
1 0 0 1
1 1 1 1
1 0 0 1 and k=3 ,
0 0 1 0
1 0 0 1 holds the property ,
1 1 1 holds the property ,
1 1 1 holds the property ,
0 0
1 0
1 1 holds the property ,
1 1
1 0 holds the property ,
1 1
0 1 holds the property ,
1
1
1 holds the property
1
1
1 holds the property
Therefore, if I did not miss something, for this example the answer should be 8.
In other words, we need to check all possible rectangles in A to see if the sum of their elements is K. Is there a way to do this faster than O (n ^ 2 * k ^ 2)?
source
share