Sponsored
Sponsored
This approach involves two distinct passes over the matrix: first to flip each row horizontally by reversing the elements, and second to invert all the elements by replacing 0s with 1s and vice versa.
Time Complexity: O(n^2) as each element is visited once.
Space Complexity: O(1) as no additional space proportional to input size is used.
1#include <stdio.h>
2
3void flipAndInvertImage(int** image, int imageSize, int* imageColSize){
4 for (int i = 0; i < imageSize; i++) {
5 int start = 0, end = imageColSize[i] - 1;
6 while (start <= end) {
7 int temp = image[i][start] ^ 1;
8 image[i][start] = image[i][end] ^ 1;
9 image[i][end] = temp;
10 start++;
11 end--;
12 }
13 }
14}
15
16int main() {
17 // Example usage
18 int image1[3][3] = { {1, 1, 0}, {1, 0, 1}, {0, 0, 0} };
19 int *imagePtrs1[3] = { image1[0], image1[1], image1[2] };
20 int imageColSize1[3] = { 3, 3, 3 };
21 flipAndInvertImage(imagePtrs1, 3, imageColSize1);
22 return 0;
23}
This C solution uses a two-pointer technique to simultaneously flip and invert each row of the binary matrix. Using XOR with 1 inverts each bit.
This approach attempts to combine the flipping and inverting processes in one pass to enhance efficiency, leveraging the symmetry characteristics of the problem.
Time Complexity: O(n^2) given each element may potentially be processed.
Space Complexity: O(1) since modifications are in place.
1#include
This C solution performs operations on symmetry pairs. When both elements in a pair are equal, they are inverted together, optimizing for fewer operations.