Find the minimum possible health of the winning player Last Updated : 10 Jul, 2022 Comments Improve Suggest changes Like Article Like Report Given an array health[] where health[i] is the health of the ith 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; } 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 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 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 JavaScript <script> // Javascript implementation of the above approach // Function to return the minimum possible // health of the last player function minHealth(health, n) { // Find the GCD of the array elements var gcd = 0; for (var i = 0; i < n; i++) { gcd = __gcd(gcd, health[i]); } return gcd; } function __gcd(a, b) { return b == 0 ? a : __gcd(b, a % b); } // Driver code var health = [ 5, 6, 1, 2, 3, 4 ]; var n = health.length; document.write(minHealth(health, n)); </script> Output: 1 Time Complexity : O(log(a+b)) ,where a and b are elements of health array. Space Complexity : O(1) ,as we are not using any extra space. Comment More infoAdvertise with us Next Article Minimum squares to evenly cut a rectangle A Arpl_09 Follow Improve Article Tags : Mathematical DSA Arrays GCD-LCM Practice Tags : ArraysMathematical Similar Reads GCD (Greatest Common Divisor) Practice Problems for Competitive Programming GCD (Greatest Common Divisor) or HCF (Highest Common Factor) of two numbers is the largest positive integer that divides both of the numbers.GCD of Two NumbersFastest Way to Compute GCDThe fastest way to find the Greatest Common Divisor (GCD) of two numbers is by using the Euclidean algorithm. The E 4 min read Program to Find GCD or HCF of Two Numbers Given two positive integers a and b, the task is to find the GCD of the two numbers.Note: The GCD (Greatest Common Divisor) or HCF (Highest Common Factor) of two numbers is the largest number that divides both of them. Examples:Input: a = 20, b = 28Output: 4Explanation: The factors of 20 are 1, 2, 4 12 min read Check if two numbers are co-prime or not Two numbers A and B are said to be Co-Prime or mutually prime if the Greatest Common Divisor of them is 1. You have been given two numbers A and B, find if they are Co-prime or not.Examples : Input : 2 3Output : Co-PrimeInput : 4 8Output : Not Co-PrimeThe idea is simple, we find GCD of two numbers a 5 min read GCD of more than two (or array) numbers Given an array arr[] of non-negative numbers, the task is to find GCD of all the array elements. In a previous post we find GCD of two number.Examples:Input: arr[] = [1, 2, 3]Output: 1Input: arr[] = [2, 4, 6, 8]Output: 2Using Recursive GCDThe GCD of three or more numbers equals the product of the pr 11 min read Program to find LCM of two numbers Given two positive integers a and b. Find the Least Common Multiple (LCM) of a and b.LCM of two numbers is the smallest number which can be divided by both numbers. Input : a = 10, b = 5Output : 10Explanation : 10 is the smallest number divisible by both 10 and 5Input : a = 5, b = 11Output : 55Expla 5 min read LCM of given array elements In this article, we will learn how to find the LCM of given array elements.Given an array of n numbers, find the LCM of it. Example:Input : {1, 2, 8, 3}Output : 24LCM of 1, 2, 8 and 3 is 24Input : {2, 7, 3, 9, 4}Output : 252Table of Content[Naive Approach] Iterative LCM Calculation - O(n * log(min(a 14 min read Find the other number when LCM and HCF given Given a number A and L.C.M and H.C.F. The task is to determine the other number B. Examples: Input: A = 10, Lcm = 10, Hcf = 50. Output: B = 50 Input: A = 5, Lcm = 25, Hcf = 4. Output: B = 20 Formula: A * B = LCM * HCF B = (LCM * HCF)/AExample : A = 15, B = 12 HCF = 3, LCM = 60 We can see that 3 * 60 4 min read Minimum insertions to make a Co-prime array Given an array of N elements, find the minimum number of insertions to convert the given array into a co-prime array. Print the resultant array also.Co-prime Array : An array in which every pair of adjacent elements are co-primes. i.e, gcd(a, b) = 1 . Examples : Input : A[] = {2, 7, 28}Output : 1Exp 6 min read Find the minimum possible health of the winning player Given an array health[] where health[i] is the health of the ith 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 winn 4 min read Minimum squares to evenly cut a rectangle Given a rectangular sheet of length l and width w. we need to divide this sheet into square sheets such that the number of square sheets should be as minimum as possible.Examples: Input :l= 4 w=6 Output :6 We can form squares with side of 1 unit, But the number of squares will be 24, this is not min 4 min read Like