Find the last player to be able to remove a string from an array which is not already removed from other array
Given two arrays of strings arr[] and brr[] of size N and M respectively, the task is to find the winner of the game when two players play the game optimally as per the following rules:
- Player 1 starts the game.
- Player 1 removes a string from the array arr[] if it is not already removed from the array brr[].
- Player 2 removes a string from the array brr[] if it is not already removed from the array arr[].
- The player who is not able to remove a string from the array, then the player will lose the game.
Examples:
Input: arr[] = { “geeks”, “geek” }, brr[] = { “geeks”, “geeksforgeeks” }
Output: Player 1
Explanation:
Turn 1: Player 1 removed “geeks” from arr[].
Turn 2: Player 2 removed “geeksforgeeks” from brr[]
Turn 3: Player 1 removed “geek” from brr[].
Now, player 2 cannot remove any string.
Therefore, the required output is Player 1.Input: arr[] = { “a”, “b” }, brr[] = { “a”, “b” }
Output: Player 2
Explanation:
Turn 1: Player 1 removed “a” from arr[].
Turn 2: Player 2 removed “b” from brr[].
Therefore, the required output is Player 2
Approach: The idea to based on the fact that common strings from both the arrays can be removed only from one of the arrays. Follow the steps below to solve the problem:
- If the count of common strings from both the arrays is an odd number, then remove one string from the array brr[], as Player 1 starts the game and the first common string is removed by Player 1.
- If count of strings in arr[] is greater than the count of strings in brr[] by removing the common strings from both the arrays, then print “Player 1”.
- Otherwise, print “Player 2”.
Below is the implementation of the above approach:
C++
// C++ Program for the above approach #include<bits/stdc++.h> using namespace std; // Function to find last player to be // able to remove a string from one array // which has not been removed from the other array void lastPlayer( int n, int m, vector<string> arr, vector<string> brr) { // Stores common strings // from both the array set<string> common; for ( int i = 0; i < arr.size(); i++) { for ( int j = 0; j < brr.size(); j++) { if (arr[i] == brr[j]) { // add common elements common.insert(arr[i]); break ; } } } // Removing common strings from arr[] set<string> a; bool flag; for ( int i = 0; i < arr.size(); i++) { flag = false ; for ( auto value : common) { if (value == arr[i]) { // add common elements flag = true ; break ; } } if (flag) a.insert(arr[i]); } // Removing common elements from B set<string> b; for ( int i = 0; i < brr.size(); i++) { flag = false ; for ( auto value : common) { if (value == brr[i]) { // add common elements flag = true ; break ; } } if (flag) b.insert(brr[i]); } // Stores strings in brr[] which // is not common in arr[] int LenBrr = b.size(); if ((common.size()) % 2 == 1) { // Update LenBrr LenBrr -= 1; } if (a.size() > LenBrr) { cout<<( "Player 1" )<<endl; } else { cout<<( "Player 2" )<<endl; } } // Driver Code int main() { // Set of strings for player A vector<string> arr{ "geeks" , "geek" }; // Set of strings for player B vector<string> brr{ "geeks" , "geeksforgeeks" }; int n = arr.size(); int m = brr.size(); lastPlayer(n, m, arr, brr); } // This code is contributed by SURENDRA_GANGWAR. |
Java
// Java Program for the above approach import java.io.*; import java.util.*; class GFG { // Function to find last player to be // able to remove a string from one array // which has not been removed from the other array static void lastPlayer( int n, int m, String[] arr, String[] brr) { // Stores common strings // from both the array Set<String> common = new HashSet<>(); for ( int i = 0 ; i < arr.length; i++) { for ( int j = 0 ; j < brr.length; j++) { if (arr[i] == brr[j]) { // add common elements common.add(arr[i]); break ; } } } // Removing common strings from arr[] Set<String> a = new HashSet<>(); boolean flag; for ( int i = 0 ; i < arr.length; i++) { flag = false ; for (String value : common) { if (value == arr[i]) { // add common elements flag = true ; break ; } } if (flag) a.add(arr[i]); } // Removing common elements from B Set<String> b = new HashSet<>(); for ( int i = 0 ; i < brr.length; i++) { flag = false ; for (String value : common) { if (value == brr[i]) { // add common elements flag = true ; break ; } } if (flag) b.add(brr[i]); } // Stores strings in brr[] which // is not common in arr[] int LenBrr = b.size(); if ((common.size()) % 2 == 1 ) { // Update LenBrr LenBrr -= 1 ; } if (a.size() > LenBrr) { System.out.print( "Player 1" ); } else { System.out.print( "Player 2" ); } } // Driver Code public static void main(String[] args) { // Set of strings for player A String[] arr = { "geeks" , "geek" }; // Set of strings for player B String[] brr = { "geeks" , "geeksforgeeks" }; int n = arr.length; int m = brr.length; lastPlayer(n, m, arr, brr); } } // This code is contributed by Dharanendra L V. |
Python
# Python Program for the above approach # Function to find last player to be # able to remove a string from one array # which has not been removed from the other array def lastPlayer(n, m, arr, brr): # Stores common strings # from both the array common = list ( set (arr) & set (brr)) # Removing common strings from arr[] a = list ( set (arr) ^ set (common)) # Removing common elements from B b = list ( set (brr) ^ set (common)) # Stores strings in brr[] which # is not common in arr[] LenBrr = len (b) if len (common) % 2 = = 1 : # Update LenBrr LenBrr - = 1 if len (a) > LenBrr: print ( "Player 1" ) else : print ( "Player 2" ) # Driver Code if __name__ = = '__main__' : # Set of strings for player A arr = [ "geeks" , "geek" ] # Set of strings for player B brr = [ "geeks" , "geeksforgeeks" ] n = len (arr) m = len (brr) lastPlayer(n, m, arr, brr) |
C#
// C# Program for the above approach using System; using System.Collections.Generic; public class GFG { // Function to find last player to be // able to remove a string from one array // which has not been removed from the other array static void lastPlayer( int n, int m, String[] arr, String[] brr) { // Stores common strings // from both the array HashSet<String> common = new HashSet<String>(); for ( int i = 0; i < arr.Length; i++) { for ( int j = 0; j < brr.Length; j++) { if (arr[i] == brr[j]) { // add common elements common.Add(arr[i]); break ; } } } // Removing common strings from []arr HashSet<String> a = new HashSet<String>(); bool flag; for ( int i = 0; i < arr.Length; i++) { flag = false ; foreach (String value in common) { if (value == arr[i]) { // add common elements flag = true ; break ; } } if (flag) a.Add(arr[i]); } // Removing common elements from B HashSet<String> b = new HashSet<String>(); for ( int i = 0; i < brr.Length; i++) { flag = false ; foreach (String value in common) { if (value == brr[i]) { // add common elements flag = true ; break ; } } if (flag) b.Add(brr[i]); } // Stores strings in brr[] which // is not common in []arr int LenBrr = b.Count; if ((common.Count) % 2 == 1) { // Update LenBrr LenBrr -= 1; } if (a.Count > LenBrr) { Console.Write( "Player 1" ); } else { Console.Write( "Player 2" ); } } // Driver Code public static void Main(String[] args) { // Set of strings for player A String[] arr = { "geeks" , "geek" }; // Set of strings for player B String[] brr = { "geeks" , "geeksforgeeks" }; int n = arr.Length; int m = brr.Length; lastPlayer(n, m, arr, brr); } } // This code is contributed by 29AjayKumar |
Javascript
<script> // JavaScript Program for the above approach // Function to find last player to be // able to remove a string from one array // which has not been removed from the other array function lastPlayer(n, m, arr, brr) { // Stores common strings // from both the array var common = []; for ( var i = 0; i < arr.length; i++) { for ( var j = 0; j < brr.length; j++) { if (arr[i] === brr[j]) { // add common elements common.push(arr[i]); j = brr.length; } } } // Removing common strings from []arr var a = []; var flag; for ( var i = 0; i < arr.length; i++) { flag = false ; common.forEach((value) => { if (value === arr[i]) { // add common elements flag = true ; i = arr.length; } }); if (flag) a.push(arr[i]); } // Removing common elements from B var b = []; for ( var i = 0; i < brr.length; i++) { flag = false ; common.forEach((value) => { if (value === brr[i]) { // add common elements flag = true ; i = brr.length; } }); if (flag) b.push(brr[i]); } // Stores strings in brr[] which // is not common in []arr var LenBrr = b.length; if (common.length % 2 === 1) { // Update LenBrr LenBrr -= 1; } if (a.length > LenBrr) { document.write( "Player 1" ); } else { document.write( "Player 2" ); } } // Driver Code // Set of strings for player A var arr = [ "geeks" , "geek" ]; // Set of strings for player B var brr = [ "geeks" , "geeksforgeeks" ]; var n = arr.length; var m = brr.length; lastPlayer(n, m, arr, brr); // This code is contributed by rdtank. </script> |
Player 1
Time Complexity: O(N + M)
Auxiliary Space: O(N + M)
Please Login to comment...