Open In App

Minimum score after flipping the cards

Given N cards having positive integers printed on the front and the back of each card (possibly different). Any number of cards can be flipped, and after that we choose one card from the deck. If the number X written on the back of the chosen card is not in the front of any card, then we say number X is good. The task is to find the smallest number that is good. If no number is good, then print 0

Note: A flip swaps the front and back numbers present on the card i.e. the value on the front is now on the back and vice versa. 



Examples:

Input: fronts = [1, 2, 4, 4, 7, 8], backs = [1, 3, 4, 1, 3, 9] 
Output: 2 If we flip the second card, the fronts are [1, 3, 4, 4, 7, 8] and the backs are [1, 2, 4, 1, 3, 9]. Now, we choose the second card, having number 2 on the back, and it isn’t on the front of any other card, so 2 is good. 



Input: fronts = [1, 2, 3, 4, 5], backs = [6, 7, 8, 9, 10] 
Output: 1

Approach:

Below is the implementation of the above approach: 




// C++ implementation of the approach
#include <bits/stdc++.h>
#define MAX 9999
 
using namespace std;
 
int flipgame(int fronts[], int backs[], int fl, int bl)
{
  set<int> same;
 
  for (int i = 0; i < fl; i++)
    if (fronts[i] == backs[i])
      same.insert(fronts[i]);
 
  //  Initialize answer to arbitrary value
  int ans = MAX;
 
  int arr[fl + bl];
  for (int i = 0; i < fl + bl; i++) {
    if (i < fl)
      arr[i] = fronts[i];
    else
      arr[i] = backs[i - fl];
  }
 
  for (int k : arr)
    if (same.find(k) == same.end())
      ans = min(ans, k);
 
  // Return final answer
  return ans % MAX;
}
 
// Driver Code
int main()
{
  int fronts[] = { 1, 2, 4, 4, 7 };
  int backs[] = { 1, 3, 4, 1, 3 };
 
  int fl = sizeof(fronts) / sizeof(fronts[0]);
  int bl = sizeof(backs) / sizeof(backs[0]);
 
  cout << flipgame(fronts, backs, fl, bl);
}
 
// This code is contributed by phasing17




// Java implementation of the approach
import java.util.*;
 
class GFG {
    static int MAX = 9999;
 
    static int flipgame(int[] fronts, int[] backs, int fl,
                        int bl)
    {
        HashSet<Integer> same = new HashSet<Integer>();
 
        for (int i = 0; i < fl; i++)
            if (fronts[i] == backs[i])
                same.add(fronts[i]);
 
        //  Initialize answer to arbitrary value
        int ans = MAX;
 
        int[] arr = new int[fl + bl];
        for (int i = 0; i < fl + bl; i++) {
            if (i < fl)
                arr[i] = fronts[i];
            else
                arr[i] = backs[i - fl];
        }
 
        for (int k : arr)
            if (!same.contains(k))
                ans  = Math.min(ans, k);
 
        // Return final answer
        return ans % MAX;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int[] fronts = { 1, 2, 4, 4, 7 };
        int[] backs = { 1, 3, 4, 1, 3 };
 
        int fl = fronts.length;
        int bl = backs.length;
 
        System.out.println(flipgame(fronts, backs, fl, bl));
    }
}
 
// This code is contributed by phasing17




# Python3 iomplementation of the approach
import itertools
 
MAX = 9999
 
def flipgame(fronts, backs):
 
    same = {k for i, k in enumerate(fronts) if k == backs[i]}
     
    # Initialize answer to arbitrary value
    ans = MAX
     
    for k in itertools.chain(fronts, backs):
        if k not in same:
            ans = min(ans, k)
 
    # Return final answer
    return ans % MAX
 
# Driver Code
fronts = [1, 2, 4, 4, 7]
backs = [1, 3, 4, 1, 3]
print(flipgame(fronts, backs))




// C# implementation of the approach
 
using System;
using System.Collections.Generic;
 
class GFG {
    static int MAX = 9999;
 
    static int flipgame(int[] fronts, int[] backs, int fl,
                        int bl)
    {
        HashSet<int> same = new HashSet<int>();
 
        for (int i = 0; i < fl; i++)
            if (fronts[i] == backs[i])
                same.Add(fronts[i]);
 
        //  Initialize answer to arbitrary value
        int ans = MAX;
 
        int[] arr = new int[fl + bl];
        for (int i = 0; i < fl + bl; i++) {
            if (i < fl)
                arr[i] = fronts[i];
            else
                arr[i] = backs[i - fl];
        }
 
        foreach(int k in arr) if (!same.Contains(k)) ans
            = Math.Min(ans, k);
 
        // Return final answer
        return ans % MAX;
    }
 
    // Driver Code
    public static void Main(string[] args)
    {
        int[] fronts = { 1, 2, 4, 4, 7 };
        int[] backs = { 1, 3, 4, 1, 3 };
 
        int fl = fronts.Length;
        int bl = backs.Length;
 
        Console.WriteLine(flipgame(fronts, backs, fl, bl));
    }
}
 
// This code is contributed by phasing17




// JavaScript implementation of the approach
let MAX = 9999
 
function flipgame(fronts, backs)
{
    let same = new Set();
     
    for (var i = 0; i < fronts.length; i++)
        if (fronts[i] == backs[i])
            same.add(fronts[i])
     
    //  Initialize answer to arbitrary value
    let ans = MAX
     
    let arr = [...fronts]
    arr.push(...backs)
     
    for (var k of arr)
        if (!same.has(k))
            ans = Math.min(ans, k)
 
    // Return final answer
    return ans % MAX
}
 
// Driver Code
let fronts = [1, 2, 4, 4, 7]
let backs = [1, 3, 4, 1, 3]
console.log(flipgame(fronts, backs))
 
// This code is contributed by phasing17

Output
2

Article Tags :