Card Flipping Game - Problem

You are given two 0-indexed integer arrays fronts and backs of length n, where the ith card has the positive integer fronts[i] printed on the front and backs[i] printed on the back.

Initially, each card is placed on a table such that the front number is facing up and the other is facing down. You may flip over any number of cards (possibly zero).

After flipping the cards, an integer is considered good if it is facing down on some card and not facing up on any card.

Return the minimum possible good integer after flipping the cards. If there are no good integers, return 0.

Input & Output

Example 1 — Basic Case
$ Input: fronts = [1,2,4,4,7], backs = [1,3,4,1,3]
Output: 2
💡 Note: Numbers 1 and 4 appear on both sides of some cards, so they can't be good. Number 2 only appears on the front of card 1, so we can flip all other cards to hide number 2 completely, making it good.
Example 2 — No Good Numbers
$ Input: fronts = [1], backs = [1]
Output: 0
💡 Note: The only number is 1, which appears on both sides of the same card. It's impossible to make any number good, so return 0.
Example 3 — Multiple Options
$ Input: fronts = [1,2,3], backs = [3,2,1]
Output: 0
💡 Note: Every number (1,2,3) appears on both sides of at least one card. Number 2 appears on both sides of card 1, and numbers 1,3 appear on both cards but opposite sides. No number can be completely hidden.

Constraints

  • n == fronts.length == backs.length
  • 1 ≤ n ≤ 1000
  • 1 ≤ fronts[i], backs[i] ≤ 2000

Visualization

Tap to expand
Card Flipping Game: Find Minimum Good NumberInitial: fronts=[1,2,4,4,7], backs=[1,3,4,1,3]12447back:1back:3back:4back:1back:3❌ Cards 0 and 2: number appears on both sides2✓ Can be made goodAnswer: 2 (minimum good number)
Understanding the Visualization
1
Initial State
All cards show front numbers, backs are hidden
2
Identify Constraints
Numbers on both sides of same card can't be good
3
Find Solution
Minimum number that can be completely hidden
Key Takeaway
🎯 Key Insight: A number is good only if it never appears on both sides of the same card
Asked in
Google 15 Amazon 12 Microsoft 8
28.4K Views
Medium Frequency
~15 min Avg. Time
867 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen