Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions Java/832_Flipping_an_image.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*

Problem Link : https://leetcode.com/problems/flipping-an-image/

Difficulty Level: Hard
Run time: 1ms
Memory: 41.7 MB

Time Complexity - O(n^n)
Space Complexity - O(1)


Example :

Input: image = [[1,1,0],[1,0,1],[0,0,0]]

Output: [[1,0,0],[0,1,0],[1,1,1]]

Explanation: First reverse each row: [[0,1,1],[1,0,1],[0,0,0]].
Then, invert the image: [[1,0,0],[0,1,0],[1,1,1]]

*/

public class 832_Flipping_an_image
{
public static void main(String[] args) {

int [][] image = {{1,1,0},
{1,0,1},{0,0,0}};

int[][] ans =flipAndInvertImage(image);

for (int i = 0; i < ans.length; i++) {
for (int a = 0; a < ans[i].length; a++) {
System.out.print(ans[i][a] + " ");
}
System.out.println();
}
}

public static int[][] flipAndInvertImage(int[][] image) {

for(int[] row : image) {
// reverse this array
for (int i = 0; i < (image[0].length + 1) / 2; i++) {
// swap the elements
int temp = row[i] ^ 1;
row[i] = row[image[0].length - i - 1] ^ 1;
row[image[0].length - i - 1] = temp;
}

/* we know 0^ 1 = 1 and 1 ^ 1 = 0, since we needed to change 0 to 1 and vice
* versa, here we used the bitwise operator to do that */

}
return image;

}

}
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
| 36 | [Valid Sudoku](https://leetcode.com/problems/valid-sudoku/) | [Java](./Java/valid-sudoku.java) | O(N^2) | O(N) | Medium | Array, 2D Matrix |
| 1512 | [Number of Good Pairs](https://leetcode.com/problems/number-of-good-pairs/) | [Java](./Java/Number-of-Good-Pairs.java) | O(N^2) | O(1) | Easy | Array |
| 162 | [Find Peak element](https://leetcode.com/problems/find-peak-element/) | [javascript](https://github.com/codedecks-in/LeetCode-Solutions/blob/master/JavaScript/findPeakElement.js) | o(Logn) | O(1) | Medium | Array |
| 54 | [Spiral Matrix](https://leetcode.com/problems/spiral-matrix/) | [C++](./C++/Spiral-matrix.cpp) | O(M\*N) | O(M\*N) | Medium | Array |
| 832 | [Flipping an Image](https://leetcode.com/problems/flipping-an-image/) | [Java](./Java/832_Flipping_an_image.java) | O(N^N) | O(1) | Hard | Array |



<br/>
Expand Down