I'm trying to invert and flip a two-dimensional array, but something goes wrong! Flipping works ok, but inverting is not. Can't find a mistake right here:
public int[][] flipAndInvert(int[][] A) {
int row = -1;
int col = -1;
int[][] arr = A;
for (int i = 0; i < arr.length; i++) {
row++;
col = -1;
for (int j = arr[i].length - 1; j >= 0; j--) {
col++;
arr[row][col] = A[i][j];
}
}
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
if (arr[i][j] == 1) {
arr[i][j] = 0;
} else {
arr[i][j] = 1;
}
}
}
return arr;
}
int[][] A = { { 0, 1, 1 },{ 0, 0, 1 },{ 0, 0, 0 } };
After proceeding the output should be: After inverting: {1,1,0},{1,0,0},{0,0,0} After flipping: {0,0,1,},{0,1,1},{1,1,1}
Thanks to all a lot, the problem was here: int[][] arr = A; The reference of the array is being passed to arr.
{ {1,1,1}, {1,1,1}, {1,1,1}from input array{ {0,1,1}, {0,1,1}, {0,1,1} }. That doesn't seem like what you want. - mypetlion