Skip to content

Commit dc04d69

Browse files
committed
One-Hundred-Fifty-Five Commit: Add Flip and Invert an Image problem to Bitwise XOR section
1 parent 2dd89ce commit dc04d69

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package Bitwise_XOR;
2+
3+
// Problem Statement: Flip and Invert an Image (hard)
4+
// LeetCode Question: 832. Flipping an Image
5+
6+
public class Problem_4_Flip_And_Invert_An_Image {
7+
public int[][] flipAndInvertImage(int[][] arr) {
8+
int C = arr[0].length; // Get the number of columns in the input matrix
9+
for (int[] row: arr) { // Iterate through each row in the matrix
10+
for (int i = 0; i < (C + 1) / 2; ++i) { // Iterate through the first half of each row
11+
int tmp = row[i] ^ 1; // Invert the current element and store it in tmp
12+
row[i] = row[C - 1 - i] ^ 1; // Invert and swap elements symmetrically from the beginning and end of the row
13+
row[C - 1 - i] = tmp; // Update the end element with the temporary value
14+
}
15+
}
16+
return arr; // Return the modified matrix
17+
}
18+
}

0 commit comments

Comments
 (0)