{"id":22513840,"url":"https://github.com/emahtab/flipping-an-image","last_synced_at":"2026-01-07T12:04:37.101Z","repository":{"id":79525515,"uuid":"231051034","full_name":"eMahtab/flipping-an-image","owner":"eMahtab","description":"Flipping an image","archived":false,"fork":false,"pushed_at":"2020-01-06T09:56:00.000Z","size":15,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-02T03:25:29.565Z","etag":null,"topics":["array","leetcode","problem-solving"],"latest_commit_sha":null,"homepage":null,"language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/eMahtab.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-12-31T07:56:45.000Z","updated_at":"2020-01-06T09:56:02.000Z","dependencies_parsed_at":"2023-05-10T17:15:35.677Z","dependency_job_id":null,"html_url":"https://github.com/eMahtab/flipping-an-image","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fflipping-an-image","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fflipping-an-image/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fflipping-an-image/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fflipping-an-image/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eMahtab","download_url":"https://codeload.github.com/eMahtab/flipping-an-image/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245952087,"owners_count":20699424,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["array","leetcode","problem-solving"],"created_at":"2024-12-07T03:14:37.177Z","updated_at":"2026-01-07T12:04:37.063Z","avatar_url":"https://github.com/eMahtab.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Flipping an image\n## https://leetcode.com/problems/flipping-an-image\n\nGiven a binary matrix A, we want to flip the image horizontally, then invert it, and return the resulting image.\n\nTo flip an image horizontally means that each row of the image is reversed.  For example, flipping [1, 1, 0] horizontally results in [0, 1, 1].\n\nTo invert an image means that each 0 is replaced by 1, and each 1 is replaced by 0. For example, inverting [0, 1, 1] results in [1, 0, 0].\n\n```\nExample 1:\n\nInput: [[1,1,0],[1,0,1],[0,0,0]]\nOutput: [[1,0,0],[0,1,0],[1,1,1]]\nExplanation: First reverse each row: [[0,1,1],[1,0,1],[0,0,0]].\nThen, invert the image: [[1,0,0],[0,1,0],[1,1,1]]\n\nExample 2:\n\nInput: [[1,1,0,0],[1,0,0,1],[0,1,1,1],[1,0,1,0]]\nOutput: [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]\nExplanation: First reverse each row: [[0,0,1,1],[1,0,0,1],[1,1,1,0],[0,1,0,1]].\nThen invert the image: [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]\n\nNotes:\n1 \u003c= A.length = A[0].length \u003c= 20\n0 \u003c= A[i][j] \u003c= 1\n```\n## Approach :\nWe will use the `two pointer technique` to reverse each row of the 2D matrix, and while reversing the rows, at the same time we will invert the 0's to 1's and 1's to 0's\n\n### 💥 XOR 💥 - Inverting cell values\nSince we are given that cell value can only be either 0 or 1.\n\nWe can use the XOR operation to invert the cell values.\n\nIf we do an XOR operation between cell value and **`1`**, we will get the inverted value as shown in table below.\n\n![XOR Operation](xor-table.PNG?raw=true \"XOR Operation\")\n\n\n### Implementation \n\n```java\npublic int[][] flipAndInvertImage(int[][] A) {\n        if(A == null)\n            return A;\n        for(int i = 0; i \u003c A.length; i++){\n            int start = 0;\n            int end = A[0].length - 1;\n            while(start \u003c end){\n                int temp = A[i][start];\n                A[i][start] = A[i][end] ^ 1;\n                A[i][end] = temp ^ 1;\n                start++;\n                end--;\n            }\n            \n            if(start == end){\n               A[i][start] = A[i][start] ^ 1 ;\n            }   \n        }\n    return A;\n}\n```\n\nAbove implementation have runtime complexity of O(r * c) and space complexity of O(1), where r is the number of rows in the input matrix and c is the number of columns in the input matrix.\n\n```\nRuntime Complexity = O(r * c)\nSpace Complexity   = O(1)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fflipping-an-image","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Femahtab%2Fflipping-an-image","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fflipping-an-image/lists"}