Make All Dominoes the Same

Make All Dominoes the Same – Minimum Rotations

Problem Statement:

You’re given two integer arrays tops[] and bottoms[] representing domino tiles.
Each tile has a top and bottom value between 1 and 6.

You can rotate any domino (i.e., swap tops[i] and bottoms[i]).

Goal:
Find the minimum number of rotations needed so that all the values in either the tops or bottoms array are the same.

If it’s impossible, return -1.

Example:

Input: tops = [2,1,2,4,2,2], bottoms = [5,2,6,2,3,2] Output: 2

Explanation: Rotate the 2nd and 4th domino so all top values become 2.

Best Data Structure for the Job:

No advanced data structures are needed here — we rely on simple counters and loops. The smart trick is to focus on just two possible candidates: tops[0] and bottoms[0].

Different Approaches

Approach 1: Brute Force

Try all numbers from 1 to 6 and check if it's possible to make all tops or all bottoms equal to that number.

public class BruteForceDomino { public int minDominoRotations(int[] tops, int[] bottoms) { int min = Integer.MAX_VALUE; for (int target = 1; target <= 6; target++) { int topRotations = 0; int bottomRotations = 0; boolean canMake = true; for (int i = 0; i < tops.length; i++) { if (tops[i] != target && bottoms[i] != target) { canMake = false; break; } else if (tops[i] != target) { topRotations++; } else if (bottoms[i] != target) { bottomRotations++; } } if (canMake) { min = Math.min(min, Math.min(topRotations, bottomRotations)); } } return min == Integer.MAX_VALUE ? -1 : min; } }

Approach 2: Optimal Greedy (Check Candidate Values Only)

We only need to check two candidates: tops[0] and bottoms[0].

public class OptimalDomino { public int minDominoRotations(int[] tops, int[] bottoms) { int rotations = check(tops[0], tops, bottoms); if (rotations != -1 || tops[0] == bottoms[0]) return rotations; return check(bottoms[0], tops, bottoms); } private int check(int target, int[] tops, int[] bottoms) { int topRotations = 0; int bottomRotations = 0; for (int i = 0; i < tops.length; i++) { if (tops[i] != target && bottoms[i] != target) { return -1; } else if (tops[i] != target) { topRotations++; } else if (bottoms[i] != target) { bottomRotations++; } } return Math.min(topRotations, bottomRotations); } }

⚠️Common Mistakes and Pitfalls

  • Trying all 6 values in optimized approach when only 2 are enough

  • Forgetting to check both top and bottom for each domino

  • Not tracking both top and bottom rotation counts separately

  • Returning the wrong rotation count (always return the minimum)

📊Time and Space Complexity

Approach         Time Complexity      Space Complexity
Brute Force         O(6 * N)      O(1)
Optimal Greedy         O(N)      O(1)

🌍Real-world Applications

  • Puzzle games and board mechanics involving tile alignment

  • Hardware systems where dual-sided components need uniform alignment

  • Data normalization problems with two possible value sets


I hope you found this article insightful! Keep exploring, keep learning, and don’t forget to check back daily for more exciting problem breakdowns. If you know someone who would benefit from this, share it with them and help them grow too! That’s it for today—see you in the next post!

                                                                                                                                         Signing off!! 

                                                                                                Master DSA One Problem at a Time :)

Comments

Popular posts from this blog

Pascal's Triangle

Divisible Drama — Who’s In, Who’s Out?

Tower of Hanoi Modified