C++ program to find winner of card game

Suppose we have a number n, two arrays A and B of size k1 and k2 respectively. Amal and Bimal are playing interesting card game. There are n cards, numbered 1 to n. Initially the cards are distributed between them. The game goes as follows: on each turn, each player takes one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. Then, both cards are revealed, and the player, whose card number is larger, takes both cards in his hand. Every card may be played any amount of times. A represents the cards Amal plays, B represents the cards that Bimal plays. The player loses if he doesn't have any cards. We have to find the final winner.

So, if the input is like n = 5; A = [3, 2]; B = [5, 1, 4], then the output will be Bimal, because initially they are playing (3, 5), Bimal takes all cards, Then plays (3, 1) Amal takes both cards, then if they play (3, 4) Bimal takes all and then if Amal plays 1, Bimal will take them with card 5 so there will be no cards in Amal's hand.


To solve this, we will follow these steps −

d := 0
e := 0
for initialize i := 0, when i < size of A, update (increase i by 1),
   f := A[i]
   if d < f, then:
      d := f
for initialize i := 0, when i < size of A, update (increase i by 1),do:
   f := A[i]
   if e < f, then:
      e := f
if d > e, then:
   return "Amal"
   return "Bimal"


Let us see the following implementation to get better understanding −

using namespace std;
string solve(int n, vector<int> A, vector<int> B){
   int d = 0;
   int e = 0;
   for(int i = 0; i<A.size(); i++){
      int f = A[i];
      if (d < f)
         d = f;
   for(int i = 0; i<A.size(); i++){
      int f = A[i];
      if(e < f)
         e = f;
   if (d > e)
      return "Amal";
      return "Bimal";
int main(){
   int n = 5;
   vector<int> A = {3, 2};
   vector<int> B = {5, 1, 4};
   cout << solve(n, A, B) << endl;


5, {3, 2}, {5, 1, 4}