# Minimum possible final health of the last monster in a game

Given **N** monsters, each monster has initial health **h[i]** which is an integer. A monster is alive if its health is greater than **0**. In each turn a random monster kills another random monster, the monster which is attacked, its health reduces by the amount of health of the attacking monster. This process is continued until a single monster is left. What will be the minimum possible health of the last remained monster. In others words, the task is to play the game in such a way that monster which is left in the end has the least possible health.

**Examples:**

Input:h[] = {2, 14, 28, 56}

Output:2

When only the first monster keeps on attacking the remaining 3 monsters, the final health of the last monster will be 2, which is minimum.

Input:h[] = {7, 17, 9, 100, 25}

Output:1

Input:h[] = {5, 5, 5}

Output:5

**Approach:** It can be observed from the problem that one has to find a certain value of health of the monster, let’s say k which can kill other monsters including self. Once this crucial observation is made problem becomes easy. Suppose we have two monsters with health **h1** and **h2**, and let’s say **h2 > h1**. We can see that in a random choice, the optimal way would be to pick a monster with lower health and reduce the health of the other monster till its health becomes less than the health of the attacking monster. After that we will pick the second monster whose health has became less than **h1** and the process will continue till only one monster is left. So at last we will be left with the minimum value which would be **gcd(h1, h2)**. This gcd method can be extended for all the monsters.

**So our resultant minimum possible health of the monster will be the gcd of all the health of given monsters i.e. H(min) = gcd(h1, h2, …, hn).**

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the gcd of two numbers ` `int` `gcd(` `int` `a, ` `int` `b) ` `{ ` ` ` `if` `(a == 0) ` ` ` `return` `b; ` ` ` `return` `gcd(b % a, a); ` `} ` ` ` `// Function to return the minimum ` `// possible health for the monster ` `int` `solve(` `int` `* health, ` `int` `n) ` `{ ` ` ` `// gcd of first and second element ` ` ` `int` `currentgcd = gcd(health[0], health[1]); ` ` ` ` ` `// gcd for all subsequent elements ` ` ` `for` `(` `int` `i = 2; i < n; ++i) { ` ` ` `currentgcd = gcd(currentgcd, health[i]); ` ` ` `} ` ` ` `return` `currentgcd; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `health[] = { 4, 6, 8, 12 }; ` ` ` `int` `n = ` `sizeof` `(health) / ` `sizeof` `(health[0]); ` ` ` `cout << solve(health, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the gcd of two numbers ` `static` `int` `gcd(` `int` `a, ` `int` `b) ` `{ ` ` ` `if` `(a == ` `0` `) ` ` ` `return` `b; ` ` ` `return` `gcd(b % a, a); ` `} ` ` ` `// Function to return the minimum ` `// possible health for the monster ` `static` `int` `solve(` `int` `health[], ` `int` `n) ` `{ ` ` ` `// gcd of first and second element ` ` ` `int` `currentgcd = gcd(health[` `0` `], health[` `1` `]); ` ` ` ` ` `// gcd for all subsequent elements ` ` ` `for` `(` `int` `i = ` `2` `; i < n; ++i) ` ` ` `{ ` ` ` `currentgcd = gcd(currentgcd, health[i]); ` ` ` `} ` ` ` `return` `currentgcd; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String args[]) ` `{ ` ` ` `int` `health[] = { ` `4` `, ` `6` `, ` `8` `, ` `12` `}; ` ` ` `int` `n = health.length; ` ` ` `System.out.println(solve(health, n)); ` `} ` `} ` ` ` `// This code is contributed by ` `// Surendra_Gangwar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the gcd of two numbers ` `def` `gcd(a, b): ` ` ` ` ` `if` `(a ` `=` `=` `0` `): ` ` ` `return` `b ` ` ` `return` `gcd(b ` `%` `a, a) ` ` ` `# Function to return the minimum ` `# possible health for the monster ` `def` `solve(health, n): ` ` ` ` ` `# gcd of first and second element ` ` ` `currentgcd ` `=` `gcd(health[` `0` `], health[` `1` `]) ` ` ` ` ` `# gcd for all subsequent elements ` ` ` `for` `i ` `in` `range` `(` `2` `, n): ` ` ` `currentgcd ` `=` `gcd(currentgcd, ` ` ` `health[i]) ` ` ` `return` `currentgcd ` ` ` `# Driver code ` `health ` `=` `[` `4` `, ` `6` `, ` `8` `, ` `12` `] ` `n ` `=` `len` `(health) ` `print` `(solve(health, n)) ` ` ` `# This code is contributed by mohit kumar ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the gcd of two numbers ` `static` `int` `gcd(` `int` `a, ` `int` `b) ` `{ ` ` ` `if` `(a == 0) ` ` ` `return` `b; ` ` ` `return` `gcd(b % a, a); ` `} ` ` ` `// Function to return the minimum ` `// possible health for the monster ` `static` `int` `solve(` `int` `[]health, ` `int` `n) ` `{ ` ` ` `// gcd of first and second element ` ` ` `int` `currentgcd = gcd(health[0], health[1]); ` ` ` ` ` `// gcd for all subsequent elements ` ` ` `for` `(` `int` `i = 2; i < n; ++i) ` ` ` `{ ` ` ` `currentgcd = gcd(currentgcd, health[i]); ` ` ` `} ` ` ` `return` `currentgcd; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String []args) ` `{ ` ` ` `int` `[]health = { 4, 6, 8, 12 }; ` ` ` `int` `n = health.Length; ` ` ` `Console.WriteLine(solve(health, n)); ` `} ` `} ` ` ` `// This code is contributed by Arnab Kundu ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of the approach ` ` ` `// Function to return the gcd of two numbers ` `function` `gcd(` `$a` `, ` `$b` `) ` `{ ` ` ` `if` `(` `$a` `== 0) ` ` ` `return` `$b` `; ` ` ` `return` `gcd(` `$b` `% ` `$a` `, ` `$a` `); ` `} ` ` ` `// Function to return the minimum ` `// possible health for the monster ` `function` `solve(` `$health` `, ` `$n` `) ` `{ ` ` ` `// gcd of first and second element ` ` ` `$currentgcd` `= gcd(` `$health` `[0], ` ` ` `$health` `[1]); ` ` ` ` ` `// gcd for all subsequent elements ` ` ` `for` `(` `$i` `= 2; ` `$i` `< ` `$n` `; ++` `$i` `) ` ` ` `{ ` ` ` `$currentgcd` `= gcd(` `$currentgcd` `, ` ` ` `$health` `[` `$i` `]); ` ` ` `} ` ` ` `return` `$currentgcd` `; ` `} ` ` ` `// Driver code ` `$health` `= ` `array` `(4, 6, 8, 12); ` `$n` `= sizeof(` `$health` `); ` `echo` `solve(` `$health` `, ` `$n` `); ` ` ` `// This code is contributed by Akanksha Rai ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

2

## Recommended Posts:

- Find the minimum possible health of the winning player
- Game Theory (Normal-form game) | Set 3 (Game with Mixed Strategy)
- Game Theory (Normal form game) | Set 2 (Game with Pure Strategy)
- Minimum Players required to win the game
- Game Theory (Normal-form Game) | Set 4 (Dominance Property-Pure Strategy)
- Game Theory (Normal-form Game) | Set 5 (Dominance Property-Mixed Strategy)
- Game Theory (Normal - form game) | Set 1 (Introduction)
- Combinatorial Game Theory | Set 2 (Game of Nim)
- Find the final X and Y when they are Altering under given condition
- Final state of the string after modification
- Final cell position in the matrix
- Find the final radiations of each Radiated Stations
- Find the final sequence of the array after performing given operations
- Find the final number obtained after performing the given operation
- Find the winner of the Game

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.