-1

I recently gave interview at a company and was rejected in final round having only one problem.

The interviewer stated a 2D-array of n*m length. We can traverse left right top down as well as both diagonally. A fixed window k was provided to find maximum sum of 1d array window traversing any of the way.

The array is not sorted and doesn't have any pattern. No overlapping/rolling is possible at edges.

1<=n,m<=10^5

Example:- 2 3 4 5 2
          3 1 8 9 9
          4 4 3 2 8 
          3 4 7 7 7
n=4
m=5
k=3

Output :- Max Sum= 26
Explanations:- (8+9+9)
              second row has the largest sum window with size 3.

I gave the brute force approach for traversing all directions(8) along with sliding window approach to calculate the max sum.

Unfortunately I was rejected and I still don't find the optimized solution for the problem made by the interviewer.

My code that I made-

(ignore the inputs required)

class sliding {
    public static void main(int ar[][], int k) {
        int m = ar.length;
        int n = ar[0].length;
        int sum = 0;

        if (m >= k) { //for row-wise max window
            for (int i = 0; i < m; i++) {
                int tempSum = 0;
                int x = 0;
                int j = 0;
                while (j < n) {
                    tempSum += ar[i][j];
                    if (j - x + 1 < k)
                        j++;
                    else if (j - x + 1 == k) {
                        sum = Math.max(tempSum, sum);
                        tempSum = tempSum - ar[i][x];
                        x++;
                        j++;
                    }
                }
            }
        }
        if (n >= k) //for column-wise max window
        {
            for (int i = 0; i < n; i++) {
                int tempSum = 0;
                int x = 0;
                int j = 0;
                while (j < m) {
                    tempSum += ar[i]][j];
                if (j - x + 1 < k)
                    j++;
                else if (j - x + 1 == k) {
                    sum = Math.max(tempSum, sum);
                    temSum = tempSum - ar[i][x];
                    x++;
                    j++;
                }
            }
        }
    }
    //for diagonal-wise max
    if (n >= k && m >= k) {
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                int x = 0;
                int p = i;
                int q = j;
                int p_initial = p;
                int q_initial = q;
                int tempSum = 0;
                while (p <= m - k && q <= n - k) {
                    if (x < k) {
                        tempSum += ar[p++][q++];
                        x++;
                    } else if (x == k) {
                        sum = Math.max(tempSum, sum);
                        tempSum -= ar[p_initial][q_initial];
                        p_initial++;
                        q_initial++;
                    }

                }
            }
        }
    }
}// sum variable will store the final answer

Complexity - O(n^3)

Can someone optimize my approach or give better solution.

3 Answers3

0

You can find the max subsequence linearly.

Given the sequence 2 3 4 5 2 with k=3 start from 2 3 4 with sum=9 and using two indexes - one pointing to 2 and one pointing to 4 - forward them modifying the sum accordingly:

  • Step 1: sum=9-2+5=12
  • Step 2: sum=12-3+2=11

This allows you to select the max linearly, which is much better than having a brute force solution

Jack
  • 1,488
  • 11
  • 21
  • I used this sliding window approach to find the maximum sum but I had to travel 8 directions thus I used brute force there, could you please help me out in finding maximum sum linearly in 2d matrix – Shubham Khatri Dec 17 '21 at 11:15
  • So you didn't implement a brute-force algorithm. What complexity analysis did you give? – Jack Dec 17 '21 at 11:16
  • yes I used brute force loops to travel row wise, column wise and 2 diagonal wise and storing the max answer in a variable. Thus I had to use sliding window approach 4 times for row, column and diagonals. My interviewer replied this is not the optimized approach. – Shubham Khatri Dec 17 '21 at 11:24
  • When you were asked for complexity, what did you answer? – Jack Dec 17 '21 at 11:46
  • `O(n²)` (or `O(nk)` more precisely) approach is to sum for every cell the next `k` elements. With sliding window for every cell you subtract and add one unit: this is `O(n)`. If you told `n²`, or coded in a `n²` way, maybe the interviewer didn't properly understood your idea of sliding the array, and correctly (imho) rejected you. (here `n` means the total number of cells, that is `n*m`) – Jack Dec 17 '21 at 11:54
  • @ShubhamKhatri: please show your code, we do not understand what you call "brute force" precisely. –  Dec 17 '21 at 13:17
  • @Jack I mentioned my code with complexity O(N^3) please help me to optimize it – Shubham Khatri Dec 19 '21 at 08:42
  • @YvesDaoust I have mentioned my code above have a read and see if you could find a better solution – Shubham Khatri Dec 19 '21 at 08:43
  • @ShubhamKhatri This answer tells you how to implement it in O(n); what part is not clear? – Jack Dec 19 '21 at 08:50
  • @Jack can you provide me the code for your approach – Shubham Khatri Dec 19 '21 at 12:25
  • @ShubhamKhatri no. But you can tell me if something is not clear and I'll try to make it clearer – Jack Dec 19 '21 at 16:04
  • @Jack I am not clear for the diagonal traversal part can you help me for diagonal traversal – Shubham Khatri Dec 23 '21 at 07:48
  • @ShubhamKhatri just play with indexes and treat a diagonal as a straight line.. i.e. `rowIdx++; colIdx--;` – Jack Dec 23 '21 at 08:44
0

To cover the main diagonal direction, use the indexings ar[i+j][j] instead of ar[i][j]. Make sure to cover the whole matrix, while ensuring 0≤i+j<n and 0≤j<m. For a given j, -j≤i<n-j so that the outer for loop is on -m<i<n. The the inner while loop is on max(0,-i)≤j<min(n-i,n).

The inner while loop keeps a similar structure and the process is still O(nm).

The second diagonal is with ar[i-j][j].

  • I liked your approach can you please mention the code for diagonal traversal only for your indexing so that it is more easy for me.... – Shubham Khatri Dec 23 '21 at 07:47
-1
public int findMaximumSum(int[][] matrix, int key) {
    int y = matrix.length - 1, x = matrix[0].length - 1;
    int max = 0;
    for (int i = 0; i <= y; i++) {
        for (int i1 = 0, j = key - 1; j <= x; j++, i1++) {
            int sum = matrix[i][i1] + matrix[i][j] + matrix[i][(i1 + j) / 2];
            max = Math.max(max, sum);
        }
    }
    return max;
}
  • I want diagonal sum as well not only row sum – Shubham Khatri Dec 17 '21 at 11:18
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 17 '21 at 11:36