Given an array **health[]** where **health[i]** is the health of the **i ^{th}** player in a game, any player can attack any other player in the game. The health of the player being attacked will be reduced by the amount of health the attacking player has. The task is to find the minimum possible health of the winning player.

**Examples:**

Input:health[] = {4, 6, 8}

Output:2

4 attacks 6, health[] = {4, 2, 8}

2 attacks 4 twice, health[] = {0, 2, 8}

2 attacks 8 four times, health[] = {0, 2, 0}

Input:health[] = {4, 1, 5, 3}

Output:1

**Approach:** In order to minimize the health of the last player, only the player with the smaller health will attack a player with the larger health and by doing so if only two players are involved then the minimum health of the last player is nothing but the GCD of the initial healths of the two players. So, the result will be the GCD of all the elements of the given array.

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 minimum possible ` `// health of the last player ` `int` `minHealth(` `int` `health[], ` `int` `n) ` `{ ` ` ` ` ` `// Find the GCD of the array elements ` ` ` `int` `gcd = 0; ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `gcd = __gcd(gcd, health[i]); ` ` ` `} ` ` ` ` ` `return` `gcd; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `health[] = { 5, 6, 1, 2, 3, 4 }; ` ` ` `int` `n = ` `sizeof` `(health) / ` `sizeof` `(` `int` `); ` ` ` ` ` `cout << minHealth(health, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the above approach ` `class` `GFG ` `{ ` ` ` `// Function to return the minimum possible ` `// health of the last player ` `static` `int` `minHealth(` `int` `health[], ` `int` `n) ` `{ ` ` ` ` ` `// Find the GCD of the array elements ` ` ` `int` `gcd = ` `0` `; ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `{ ` ` ` `gcd = __gcd(gcd, health[i]); ` ` ` `} ` ` ` `return` `gcd; ` `} ` ` ` `static` `int` `__gcd(` `int` `a, ` `int` `b) ` `{ ` ` ` `return` `b == ` `0` `? a : __gcd(b, a % b); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String []args) ` `{ ` ` ` `int` `health[] = { ` `5` `, ` `6` `, ` `1` `, ` `2` `, ` `3` `, ` `4` `}; ` ` ` `int` `n = health.length; ` ` ` ` ` `System.out.println(minHealth(health, n)); ` `} ` `} ` ` ` `// This code is contributed by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` `from` `math ` `import` `gcd ` ` ` `# Function to return the minimum possible ` `# health of the last player ` `def` `minHealth(health, n) : ` ` ` ` ` `# Find the GCD of the array elements ` ` ` `__gcd ` `=` `0` `; ` ` ` ` ` `for` `i ` `in` `range` `(n) : ` ` ` `__gcd ` `=` `gcd(__gcd, health[i]); ` ` ` ` ` `return` `__gcd; ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `health ` `=` `[ ` `5` `, ` `6` `, ` `1` `, ` `2` `, ` `3` `, ` `4` `]; ` ` ` `n ` `=` `len` `(health); ` ` ` ` ` `print` `(minHealth(health, n)); ` ` ` `# This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the above approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the minimum possible ` `// health of the last player ` `static` `int` `minHealth(` `int` `[]health, ` `int` `n) ` `{ ` ` ` ` ` `// Find the GCD of the array elements ` ` ` `int` `gcd = 0; ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` `gcd = __gcd(gcd, health[i]); ` ` ` `} ` ` ` `return` `gcd; ` `} ` ` ` `static` `int` `__gcd(` `int` `a, ` `int` `b) ` `{ ` ` ` `return` `b == 0 ? a : __gcd(b, a % b); ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String []args) ` `{ ` ` ` `int` `[]health = { 5, 6, 1, 2, 3, 4 }; ` ` ` `int` `n = health.Length; ` ` ` ` ` `Console.WriteLine(minHealth(health, n)); ` `} ` `} ` ` ` `// This code is contributed by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

**Output:**

1

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Two player game in which a player can remove all occurrences of a number
- Minimum possible final health of the last monster in a game
- Probability of A winning the match when individual probabilities of hitting the target given
- Find probability that a player wins when probabilities of hitting the target are given
- Find the player who will win the Coin game
- Find the player who wins the game by removing the last of given N cards
- Largest odd divisor Game to check which player wins
- Minimum LCM and GCD possible among all possible sub-arrays
- Minimum possible value T such that at most D Partitions of the Array having at most sum T is possible
- Flip minimum signs of array elements to get minimum sum of positive elements possible
- Find minimum elements after considering all possible transformations
- Find minimum possible digit sum after adding a number d
- Find the maximum possible value of the minimum value of modified array
- Find the value of N when F(N) = f(a)+f(b) where a+b is the minimum possible and a*b = N
- Find minimum possible values of A, B and C when two of the (A + B), (A + C) and (B + C) are given
- Find two numbers with the given LCM and minimum possible difference
- Find a number such that maximum in array is minimum possible after XOR
- Find the minimum number possible by changing at most one digit
- Represent a number as sum of minimum possible psuedobinary numbers
- Minimum Possible value of |ai + aj - k| for given array and k.

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.