Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find elements surrounding an element in an array

I have a multidimensional array, I want to get the elements surrounding a particular element in that array.

For example if I have the following:

[[1,2,3,4,5,6]
 [8,9,7,5,2,6]
 [1,6,8,7,5,8]
 [2,7,9,5,4,3]
 [9,6,7,5,2,1]
 [4,7,5,2,1,3]]

How do I find all the 8 elements around any of the above elements? And how do I take care of elements at the edges?

One way I figured out is, to write a 9 line code for this , which is obvious, but is there a better solution?

like image 557
md1hunox Avatar asked Oct 05 '12 09:10

md1hunox


2 Answers

You can use 'direction array' in form

[[-1,-1], [-1,0],[1,0]..and so on]

And method which takes point coordinate and iterates through direction array -> add direction numbers to coordinates, check indexes are not out of bounds and collect results. Something like this:

private static int[][] directions = new int[][]{{-1,-1}, {-1,0}, {-1,1},  {0,1}, {1,1},  {1,0},  {1,-1},  {0, -1}};

static List<Integer> getSurroundings(int[][] matrix, int x, int y){
    List<Integer> res = new ArrayList<Integer>();
    for (int[] direction : directions) {
        int cx = x + direction[0];
        int cy = y + direction[1];
        if(cy >=0 && cy < matrix.length)
            if(cx >= 0 && cx < matrix[cy].length)
                res.add(matrix[cy][cx]);
    }
    return res;
}
like image 104
zvez Avatar answered Oct 13 '22 01:10

zvez


For (i, j) ->

              (i - 1, j - 1)
              (i - 1, j)
              (i - 1, j + 1)

              (i, j - 1)
              (i, j + 1)

              (i + 1, j - 1)
              (i + 1, j)
              (i + 1, j + 1)

Now, at the edges, you can check for num % row == 0, then its at row edge... and , num % col == 0 then its column edge..

Here's is how you can proceed: -

Given an index (i, j).. You can find elements in a rows adjacent to j for i - 1, then i, and then i + 1. (NOTE : - for index i you just have to access j - 1, and j + 1)

Subsequently you also can check for the row edge and column edge..

Here, you can look at the code below, how it can happen: -

    // Array size
    int row = 6;
    int col = 6;
    // Indices of concern
    int i = 4;
    int j = 5;

    // To the left of current Column
    int index = i - 1;
    for (int k = -1; k < 2; k++) {
        if (index % row > 0 && ((j + k)  % col) > 0) {
            System.out.println(arr[index][j + k]);
        }
    }


    // In the current Column
    index = i;

    // Increment is 2 as we don't want (i, j)
    for (int k = -1; k < 2; k = k + 2) {            
        if (index % row > 0 && ((j + k)  % col) > 0) {
            System.out.println(arr[index][j + k]);
        }
    }

    // To the right of current Column
    index = i + 1;
    for (int k = -1; k < 2; k++) {
        if (index % row > 0 && ((j + k)  % col) > 0) {
            System.out.println(arr[index][j + k]);
        }

    }

UPDATE : - The above code can further be simplified.. But I leave that task to you.. HINT: - You can reduce one for loop from there..

like image 27
Rohit Jain Avatar answered Oct 13 '22 01:10

Rohit Jain