Simple C++ Explanation with Solution | O(n)
If it is possible to make any row same, there are only 2 options i.e either with A[0] or B[0].\nSo we check for both one by one, and return the minimum rotation
If it is possible to make any row same, there are only 2 options i.e either with A[0] or B[0].\nSo we check for both one by one, and return the minimum rotations.\n\n```\nclass Solution {\npublic:\n    \n    int helper(vector<int>& A,vector<int>& B,int x){\n        \n        int total = 0;     \n        int countA = 0;   /// No. of x in A\n        int countB = 0;  /// No. of x in B\n        for(int i = 0 ;i<A.size();i++){\n            if(A[i] == x || B[i] == x)\n                total++;\n            if(A[i] == x)\n                countA++;\n            if(B[i] == x)\n                countB++;\n        }\n        \n        if(total != A.size())   /// If total is not equal to A.size(), then it is not possible.\n            return -1;\n        \n        else return min(total - countA,total - countB);      \n        \n    }\n    \n    \n    int minDominoRotations(vector<int>& A, vector<int>& B) {\n        \n        int ans = helper(A,B,A[0]);   /// helper function helps find the minimum rotations needed to make all elements of A or B = x.\n        if(ans != -1)   /// We return here without checking for B[0] because even if the other row becomes all B[0], minimum rotations wold be the same.\n            return ans;\n        else return helper(A,B,B[0]);\n        \n    }\n};\n```
['C']