
- 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++ code to find final number after min max removal game
Suppose we have an array A with n elements. There are n numbers written on a board. Amal and Bimal are playing a turn based game. In each turn, they select a number and remove it from board. Amal plays first. Amal wants to minimize the last number that he would left on the board, and Bimal wants to maximize it. We have to find the number which will remain on the board.
So, if the input is like A = [2, 1, 3], then the output will be 2, because Amal will remove 3, Bimal will remove 1, so the final number will be 2.
Steps
To solve this, we will follow these steps −
n := size of A sort the array A return A[floor of ((n - 1)/2)]
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; int solve(vector<int> A){ int n = A.size(); sort(A.begin(), A.end()); return A[(n - 1) / 2]; } int main(){ vector<int> A = { 2, 1, 3 }; cout << solve(A) << endl; }
Input
{ 2, 1, 3 }
Output
2
- Related Articles
- C++ code to find corrected text after double vowel removal
- Program to fill Min-max game tree in Python
- C++ program to find winner of ball removal game
- C++ Program to find maximum score of bit removal game
- C++ code to find minimum number starting from n in a game
- C++ Program to find array after removal from maximum
- C program to find sum, max and min with Variadic functions
- Program to find winner of array removal game in Python
- C++ code to find max ornaments to make decoration good
- Convert min Heap to max Heap in C++
- Min-Max Heaps
- Program to find maximum score of brick removal game in Python
- C++ code to find out who won an n-round game
- C++ code to find minimal tiredness after meeting
- C++ program to find reduced size of the array after removal operations

Advertisements