1

I have a following 2 dimensional array:

int[][] array = new int[][]{
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 1, 1, 1, 0, 0, 0, 0},
        {0, 0, 0, 1, 1, 1, 1, 0, 0, 0},
        {0, 0, 1, 1, 0, 1, 1, 0, 0, 0},
        {0, 0, 1, 1, 0, 1, 0, 0, 0, 0},
        {0, 0, 0, 1, 1, 1, 1, 0, 0, 0},
        {0, 0, 0, 1, 1, 1, 1, 0, 0, 0},
        {0, 0, 0, 0, 0, 1, 0, 0, 0, 0}
};

and I would like to trim all the surrounding zeroes, so my output would be like this (removing "zeros" outside and preserving the zeroes that are surrounded by "ones"):

        {0, 1, 1, 1, 0},
        {0, 1, 1, 1, 1},
        {1, 1, 0, 1, 1},
        {1, 1, 0, 1, 0},
        {0, 1, 1, 1, 1},
        {0, 1, 1, 1, 1},
        {0, 0, 0, 1, 0}

I'm looking for an efficient way of doing this.

Jan Slominski
  • 2,968
  • 4
  • 35
  • 61
  • 3
    Have you tried anything? – XtremeBaumer Jun 19 '18 at 12:16
  • 1
    Rather than looking at surrounding zeroes or ones, it seems that your algo consists in removing all the lines and columns where the sum of all items is 0. – assylias Jun 19 '18 at 12:18
  • 1
    1. [Find the coordinates of the inner 2D array](https://stackoverflow.com/questions/30697790/2d-array-trim-with-specified-value), 2. [Copy it in a new variable](https://stackoverflow.com/questions/16362872/how-to-get-2d-subarray-from-2d-array-in-java) – Oneiros Jun 19 '18 at 12:30
  • @assylias Or maybe only if they come first or last. – Ole V.V. Jun 19 '18 at 12:33
  • @OleV.V. the first and last two columns are supposed to be removed so... maybe it's a `first and last N` trick, or maybe it's the sum :) – Shark Jun 19 '18 at 12:49
  • I have voted to close this question because the requirements are unclear, and the question neither shows any search and research effort nor any attempt to solve the problem. – Ole V.V. Jun 19 '18 at 13:41

1 Answers1

3

Possible solution (dunno if it is the most efficient way):

public static int[][] trim(int[][] mtx, int rmin, int rmax, int cmin, int cmax) {
   int[][] result = new int[rmax-rmin+1][];
   for (int r = rmin, i = 0; r <= rmax; r++, i++) {
      result[i] = Arrays.copyOfRange(mtx[r], cmin, cmax+1);
   }
   return result;
}

public static int[][] trim(int[][] mtx, int trimmed) {
   int cmin = mtx[0].length;
   int rmin = mtx.length;
   int cmax = -1;
   int rmax = -1;

   for (int r = 0; r < mtx.length; r++)
      for (int c = 0; c < mtx[0].length; c++)
         if (mtx[r][c] != trimmed) {
            if (cmin > c) cmin = c;
            if (cmax < c) cmax = c;
            if (rmin > r) rmin = r;
            if (rmax < r) rmax = r;
         }

   return trim(mtx, rmin, rmax, cmin, cmax);
}

public static void main (String[] args) {
   int[][] array = new int[][]{
      {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
      {0, 0, 0, 1, 1, 1, 0, 0, 0, 0},
      {0, 0, 0, 1, 1, 1, 1, 0, 0, 0},
      {0, 0, 1, 1, 0, 1, 1, 0, 0, 0},
      {0, 0, 1, 1, 0, 1, 0, 0, 0, 0},
      {0, 0, 0, 1, 1, 1, 1, 0, 0, 0},
      {0, 0, 0, 1, 1, 1, 1, 0, 0, 0},
      {0, 0, 0, 0, 0, 1, 0, 0, 0, 0}
   };
   int[][] trim = trim(array, 0);
   System.out.println(Arrays.deepToString(trim));
}
Oneiros
  • 4,328
  • 6
  • 40
  • 69