Naive and Optimized approach (100 %) - Java
# Naive Approach (19ms) & Optimized Solution(0ms) -- Main Concepts covered\n\nNaive Solution\nThe idea is simple we first have to reverse each array and then we
**# Naive Approach (19ms) & Optimized Solution(0ms) -- Main Concepts covered**\n\n***Naive Solution***\nThe idea is simple we first have to reverse each array and then we change the each element 1-->0 & 0-->1\n\nFor reversing the array  we use **2-pointer apparoch**  where we keep a low and high pointer at two ends of the array and start swapping both the pointer till (low<high) and increase the low pointer by 1 (i.e. low+=1) and reduce the high pointer by 1 (i.e. high-=1) \nNow the swap function without using temporary varibale we have 3 common approaches :\n1.    `x = x + y;\n        y = x - y;\n        x = x - y;`\n\t\t\n2.    `x = x * y; \n        y = x / y; \n        x = x / y; ` \n\t\t**2nd approach cannot be performed as we are dealing with 1 & 0 and we might end up getting                    ArithmeticException**\n\t3.  Using ***Bitwise XOR*** operator which simply does the following operation **(1^1=0 , 0^0=0 , 1^0=1 , 0^1=1)**.\n\t\t Eg. Code to swap x=5 i.e. (0101) and y=10 i.e. (1010)\n\t\t\t`x = x ^ y;`     x now becomes 15 (1111)\n\t\t\t`y = x ^ y;`     y becomes 5 (0101)\n\t\t\t`x = x ^ y;`   x becomes 10 (1010)\n\t\t\nAfter reversing the array we have to flip 1--->0  &  0 --->1, we do that using same bitwise XOR. \nTo change  1-->0 and 0-->1 most elegant way is to bitwise XOR the number with 1.\n\t**0 ^ 1 = 1\n\t1 ^ 1 = 0**\n\n**Naive Solution :- Time complexity: O(n^3)  Space Complexity: O(n^2)**\n```\npublic int[][] flipAndInvertImage(int[][] image) {\n            for(int i=0;i<image.length;i++){\n            int low=0;\n            int high=image[i].length-1;\n            while(low<high){\n            swap(image[i][low],image[i][high],image,i,low,high);\n            low++;\n            high--;\n            }\n            for(int j=0;j<image[i].length;j++){\n                image[i][j]=image[i][j]^1;\n            }\n        }\n        return image;\n    }\n    public static void swap(int a,int b,int image[][],int i,int low,int high){\n        System.out.println(a+" "+b);\n        a=a+b;\n        b=a-b;\n        a=a-b;\n        image[i][low]=a;\n        image[i][high]=b;\n    }\n```\n\n**Optimized Solution Time Complexity : O(n^2) Space Complexity : O(n^2)**\n\nHere we again use 2 pointer to check the two ends of the array whether they are same or not. If same we store the XOR at these pointer places and move the pointer respectively as mentioned above.\n\n```\npublic int[][] flipAndInvertImage(int[][] image) {\n\t\tint len=image.length;\n        for(int i=0;i<len;i++){\n            for(int j=0;j*2<image[i].length;j++){\n                if(image[i][j]==image[i][len-j-1]){\n                    image[i][len-j-1]^=1;\n                    image[i][j]=image[i][len-j-1];\n                }\n            }\n        }\n        return image;\n    }\n```\n\n**Understanding the naive approach helps in breaking the problem into smaller problems which eventually helps in understanding how it can be optimized. of how code work is important\nUpvote if  it was helpful \uD83D\uDC4D.**\n\n\t
['Java']