0

I have a question. Can anyone help me with finding duplicates in submatrices? I have a code which finds submatrices in 2d matrix, but I can't find duplicates. I thought to push the values onto the Stack (because in assignment I should use Stack), find all duplicates in each submatrix, and then compare them, but I don't really know how to do it. I'll be very gratefull, if anyone help me to finish this program.

My code:

public static void main(String[] args) throws Exception {
        int[][] data = new int[3][3];
        Random random = new Random();
        for(int i=0; i<data.length;i++)
        {
            for(int j=0; j<data.length; j++)
            {
                data[i][j] = random.nextInt(10);
            }
        }
        printSubMatrix(data);
    }

private static void printSubMatrix(int[][] mat) {
        int rows=mat.length;
        int cols=mat[0].length;
        Stack _stack = new Stack();
        //prints all submatrix greater than or equal to 2x2
        for (int subRow = rows; subRow >= 2; subRow--) {
            int rowLimit = rows - subRow + 1;
            for (int subCol = cols; subCol >= 2; subCol--) {
                int colLimit = cols - subCol + 1;
                for (int startRow = 0; startRow < rowLimit; startRow++) {
                    for (int startCol = 0; startCol < colLimit; startCol++) {
                        for (int i = 0; i < subRow; i++) {
                            for (int j = 0; j < subCol; j++) {
                                System.out.print(mat[i + startRow][j + startCol] + " ");
                                _stack.push(mat[i+startRow][j+startCol]);
                            }
                            System.out.print("\n");
                        }
                        System.out.print("\n");
                    }
                }
            }
        }
    System.out.printf(_stack.toString().replaceAll("\\[", "").replaceAll("]", ""));
    }
  • you need to provide information on what do you mean by `duplicates in submatrix` ? There are several submatrices starting with size `1` to `m x n` do you mean to find duplicates in all those possible submatrices? and then what do you mean by "compare them"? – SomeDude Nov 14 '19 at 18:25
  • Duplicates in submatrix are the same elements. Thats mean, I need to find duplicates in first submatrix and in the second and so on. Next I need to find out which submatrix has the largest number of duplicates – YauheniLazakovich Nov 14 '19 at 19:17
  • Problem description is not clear, give nice examples if you need a solution. – User_67128 Nov 14 '19 at 21:04

0 Answers0