
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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.
Steps
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), do: 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" Otherwise return "Bimal"
Example
Let us see the following implementation to get better understanding −
#include<bits/stdc++.h> 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"; else return "Bimal"; } int main(){ int n = 5; vector<int> A = {3, 2}; vector<int> B = {5, 1, 4}; cout << solve(n, A, B) << endl; }
Input
5, {3, 2}, {5, 1, 4}
Output
Bimal
- Related Articles
- Program to find winner of stone game in Python
- C++ program to find winner of cell coloring game
- C++ program to find winner of ball removal game
- C++ Program to find winner of unique bidding game
- Program to find winner of array removal game in Python
- Program to find winner of number reducing game in Python
- C++ Program to find winner name of stick crossing game
- Program to find the winner of an array game using Python
- Program to find winner of a rower reducing game in Python
- Program to find winner of a rower breaking game in Python
- C++ program to find winner of typing game after delay timing
- Python program to find score and name of winner of minion game
- Program to find winner of a set element removal game in Python
- Card Flipping Game in C++
- Predict the winner in Coin Game in C++
