Find four consecutive same numbers diagonal wise, row wise and column wise?


I had earlier written method for such a case in 1d array

public static boolean isConsecutiveFour(int[] values) {
        int currentNumber;
        int prevNumber = values[0];
        int count = 1;
        for (int i = 1; i < values.length; i++) {
            currentNumber = values[i];
            if (prevNumber == currentNumber) {
                count++;
                if (count == 4) {
                    return true;
                }
            } else {
                count = 1; // reset count if not consecutive numbers
            }
            prevNumber = currentNumber;
        }
        return false;


    }

Now, my logic tells me that I can just do this:

       for (int i = 0; i < matrix.length; i++) {
            if (isConsecutiveFour(matrix[i])) {
                System.out.println("Consecutive four digits on " + i + " th row");
            }
        }

However, I was surprised when this didn’t work for columns.

Can nyone guide me?

You could handle each of the four cases separately. Handle the rows separately, columns separately, diagonal , and diagonal /.

That could be the first brute-force approach to consider.

public static boolean rowHasConsecutiveNumbers(int[][] matrix) {
    int counter; // random value to initialize to avoid errors
    for (int i = 0; i < 6; i++) {
        counter = 1;
        for (int j = 0; j < matrix[0].length - 1; j++) {
            for (int jfirst = j; jfirst < j + 3 - 1; j++) {
                if (matrix[i][jfirst] == matrix[i][jfirst + 1]) {
                    counter++;
                    if (counter >= 4) {
                        return true;
                    }
                }
            }
        }
    }
    return false;
}

Wrote it for rows, somehow this just works. Could not do the same for columns

It was wrong. Consulted chatgpt(first time) and corrected the code.

public class Lr {
    public static void main(String[] args) {
        boolean hasConsecutiveRows = false;
        int[][] matrix = {
                {0, 2, 0, 0, 1, 6, 1},
                {0, 1, 6, 8, 6, 0, 1},
                {5, 3, 2, 1, 8, 2, 9},
                {6, 3, 6, 1, 1, 9, 1},
                {1, 3, 6, 1, 4, 0, 7},
                {3, 3, 3, 3, 4, 0, 7}};

//        boolean doesRowHasConsecutiveNumbers = rowHasConsecutiveNumbers(matrix);
//        System.out.println(doesRowHasConsecutiveNumbers);

        boolean doesColHasConsecutiveNumbers = colHasConsecutiveNumbers(matrix);

        System.out.println(doesColHasConsecutiveNumbers);

    }

    public static boolean colHasConsecutiveNumbers(int[][] matrix) {
        for (int j = 0; j < matrix[0].length; j++) {
            int counter = 1;
            for (int i = 0; i < matrix.length - 1; i++) {
                if (matrix[i][j] == matrix[i + 1][j]) {
                    counter++;
                    if (counter >= 4) {
                        return true;
                    }
                } else {
                    counter = 1;
                }
            }

        }
        return false;
    }


    public static boolean rowHasConsecutiveNumbers(int[][] matrix) {
        for (int i = 0; i < matrix.length; i++) { // Loop through rows
            int counter = 1; // Start with a counter of 1
            for (int j = 0; j < matrix[0].length - 1; j++) { // Loop through columns
                if (matrix[i][j] == matrix[i][j + 1]) {
                    counter++; // Increment counter if consecutive numbers match
                    if (counter >= 4) {
                        return true; // Found four consecutive numbers
                    }
                } else {
                    counter = 1; // Reset counter if sequence breaks
                }
            }
        }
        return false; // No row has four consecutive equal numbers
    }
}
1 Like

You may find claude to be a better option for coding questions like this!

Thanks I had heard about it but wasn’t using it. I’ll use i from now on.

1 Like