✅[C++] | 2 Solutions  | Simple solution with explanation | O(N)
The problem says that we have to make Tops elements equal or bottoms elements equal, with minimum number of steps.\n\n\nSolution 1\n\nExplanation\n1. We will co
The problem says that we have to make Tops elements equal or bottoms elements equal, with minimum number of steps.\n<hr>\n\n**Solution 1**\n\n**Explanation**\n1. We will consider three vectors countA, count B, same\n2. countA is the occurenece of each element in tops\n3. countB is the occurenece of each element in bottoms\n4. same is occurenece of elements is both are same in tops and bottoms at same index\n5. For the first iteration we will fill countA, countB, same\n6. For the next iteration we will check from 1 to 6 that countA+countB-same==n\n7. if countA+countB-same==n return n-max(countA,countB)\n\nQ. Why we are counting countA, countB, same?\nAns: Because if any number occured in tops or bottoms n times then that element is possibility to fill tops or bottoms equally.\n\nQ what is the meaning of countA+countB-same==n ?\nAns: As we know if at same index tops[i] == bottoms[i] then we don\'t need to swap, so when we count occurenve of element in tops and bottom this same index is also considered so we have to delete it. \nIts simple meaning is that if the occurence of any element in tops and bottoms is equal to n then this elament will fill tops or bottoms.\n\n**Full explanation**\n![image](https://assets.leetcode.com/users/images/dbba83a4-e9f7-47cd-b5e5-a6022c4c639e_1647741033.2557464.jpeg)\n\n\n**CODE**\n**Time complexity: O(N)**\n**Space complexity: O(1)**\n\n```\nclass Solution {\npublic:\n    int minDominoRotations(vector<int>& tops, vector<int>& bottoms) {\n        int n=tops.size();\n        //Count the occurence of each number in tops row\n        vector<int> countA(7,0);\n        //Count the occurence of each number in bottom row\n        vector<int> countB(7,0);\n        //Count the occurence of numbers which are same in tops and bottom at same index\n        vector<int> same(7,0);\n        \n        //Iterate from 0 to n\n        for(int i=0;i<n;i++){\n            //Increment the count of tops[i]\n            countA[tops[i]]++;\n            //Increment the count of bottoms[i]\n            countB[bottoms[i]]++;\n            //Increment the count if both are same\n            if(tops[i]==bottoms[i])\n                same[tops[i]]++;\n        }\n        \n        for(int i=0;i<7;i++){\n            if((countA[i]+countB[i]-same[i])==n){\n                return n-max(countA[i],countB[i]);\n            }\n        }\n        return -1;\n    }\n};\n```\n\n**Solution 2**\n\n**Explanation**\nIf we see the problem again then we see then we get to know that tops or bottoms can only be filled with tops[0] ot bottoms[0], means we have only two possiblity either tops elements are equal to tops[0] ot bottoms elements are equal to bottoms[0].\n\n1. We will consider countA, countB\n2. countA means if the index in tops is not equal to first.\n3. countB means if the index in bottoms is not equal to first.\n4. Iterate from 0 to n check tops[i]==tops[0] other wise increment countA and bottoms[i]==bttoms[0] otherwise increment countB if we reach last index then yes it can be filled with tops[0]\n5.  Iterate from 0 to n check tops[i]==bottoms[0] other wise increment countA and bottoms[i]==bottoms[0] otherwise increment countB if we reach last index then yes it can be filled with bottoms[0]\n\n**Full explanation**\n![image](https://assets.leetcode.com/users/images/b691fac8-0b0b-459a-937b-9cf48eb9c481_1647741054.8790216.jpeg)\n\n\n**CODE**\n**Time complexity: O(N)**\n**Space complexity: O(1)**\n\n```\nclass Solution {\npublic:\n    int minDominoRotations(vector<int>& tops, vector<int>& bottoms) {\n        int n=tops.size();\n        //Count no of elements which are not equal to first element in tops\n        int countA=0;\n        //Count no of elements which are not equal to first element in bottoms\n        int countB=0;\n        //Iterate from 0 to n\n        for(int i=0;i<n;i++){\n            //If any of tops[i] or bottoms[i] are equal to tops[0]\n            if(tops[i]==tops[0] || bottoms[i]==tops[0]){\n                //Increment when not same in tops\n                if(tops[i]!=tops[0])\n                    countA++;\n                //Increment when not same in bottoms\n                if(bottoms[i]!=tops[0])\n                    countB++;\n                //If we reah last element means we can create the tops with same elements\n                if(i==n-1)\n                    return min(countA,countB);\n            }else{\n                break;\n            }\n        }\n        countA=0;\n        countB=0;\n        for(int i=0;i<n;i++){\n            //If any of tops[i] or bottoms[i] are equal to bottoms[0]\n            if(tops[i]==bottoms[0] || bottoms[i]==bottoms[0]){\n                //Increment when not same in tops\n                if(tops[i]!=bottoms[0])\n                    countA++;\n                //Increment when not same in bottoms\n                if(bottoms[i]!=bottoms[0])\n                    countB++;\n                //If we reah last element means we can create the bottoms with same elements\n                if(i==n-1)\n                    return min(countA,countB);\n            }else{\n                break;\n            }\n        }\n        return -1;\n    }\n};\n```\n\n<hr>\n<hr>\n\uD83D\uDC31\u200DIf there are any suggestions / questions / mistakes in my post, please do comment below \uD83D\uDC47\n<hr>\n<hr>
['C']