-1

I can find consecutives in the same row as follow:

int count = 0;
for (int column = 0; column < list[currentRow].length; column++) 
     if (list[currentRow][column].equals(currentValue))
         count++;

And consecutives in the same column as follow:

for (int row = currentRow; row < list.length; row++) 
        if (list[row][currentColumn].equals(currentValue))
            count++;

How to find consecutives in the same diagonal?

My tries :

for (int majorDiagonal = currentRow + 1, column = currentColumn; majorDiagonal < list.length; majorDiagonal++, column++) {
        if (list[majorDiagonal][column].equals(currentValue))
            countMajorDiagonal++;
    }

And:

for (int subDiagonal = 0; subDiagonal < list.length; subDiagonal++) {
        if (list[subDiagonal][(list.length - subDiagonal) - 1].equals(currentValue))
            countMajorSubDiagonal++;

    }

3 Answers3

0

Saying your current value is at (i, j), in order to now if there are consecutives diagonals on this value you have to check if this value is equal to values at (i-1, j-1), (i+1, j-1), (i-1, j+1) and (i+1, j+1)

And don't forget to take care of special cases when on the borders for example.

If you want to browse the whole array, you can check only right and bottom diagonaly as the left and top will have been already done by the previous iterations.

edit: What does finding consecutives means to you ? Because your function finding consecutives will only count the number of items that have the same value as the current one, you should be updating the current to the next before going back on the loop.

On A = [0, 0, 1, 1, 1, 0, 1, 0, 1, 0] your code will give 5 if currentValue = 0 before the loop and 4 if currentValue takes the first value.

Joachim Huet
  • 422
  • 3
  • 10
0

Try thinking about a diagonal as a grid:

  0 1 2 3 4 5
0 o o o o o o
1 o o o o o o
2 x o o o o o
3 o x o o o o
4 o o x o o o
5 o o o x o o  
6 o o o o x o

For the diagonal highlighted by x, the array's indices are

list[0][2]
list[1][3]
list[2][4]
list[3][5]
list[4][6]

What relationship can you see between the two indices? They always follow the format of y = x + 2, where 2 is the difference between your first 2 indices.

Whenever doing these diagonal problems, you just need to treat them as a math problem, figure out if you're using a -x or +x (As in a down diagonal or up diagonal), and then increment your counter accordingly.

So your for loop might look like:

int initial_col = // the x coordinate that you're starting from ;
int initial_row = // the y coordinate that you're starting from ;
for( int x = initial_col ; x < list.length && x + (initial_col - initial_row) < list[0].length ; i++){
  System.out.println("x index: " + x); //Check it for debugging
  int y = x + (initial_col - initial_row); //This is the y = x + b problem we looked at earlier
  System.out.println("y index: " + y); //Also for debugging
  list[x][y] ... // What you want to do with that point
}

This will probably need a little tweaking depending on your edge cases and such, but the idea is to figure out the relationship between your indices and solve it like a math problem.

Davy M
  • 1,697
  • 4
  • 20
  • 27
0
// test for dups hor, vert and diag
for(int x = 0; x < 5; x++){
    for(int y = 0; y < 5; y++){
        System.out.println("row:"+x+" col:"+y+" = "+ma[x][y]);
        // test for consecutives in a row if y is vert
            if(y < 4){
                if(ma[x][y] == ma[x][y+1]){
                    System.out.println("Found Consecutive ROW on:"+"row:"+x+" col:"+y+" of "+ma[x][y]);
                }
              }
              // test for consecutives in a col if x is col
               if(x < 4){
                 if(ma[x][y] == ma[x+1][y]){
                  System.out.println("Found Consecutive COL on:"+"row:"+x+" col:"+y+" of "+ma[x][y]);
                 }
               }
              //test vert left to right
               if(x < 4 && y < 4){
                if(ma[x][y] == ma[x+1][y+1]){
                  System.out.println("Found Consecutive Vertical Left to Right on:"+"row:"+x+" col:"+y+" of "+ma[x][y]);
                }
               }
              //test vert right to left
               if(x < 4 && y > 0){
                 if(ma[x][y] == ma[x+1][y-1]){
                  System.out.println("Found Consecutive Vertical Right to Left on:"+"row:"+x+" col:"+y+" of "+ma[x][y]);
                 }
               }
             }
            }
Ptyler649
  • 69
  • 1