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++
#include <bits/stdc++.h>
using namespace std;
int gcd( int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
int solve( int * health, int n)
{
int currentgcd = gcd(health[0], health[1]);
for ( int i = 2; i < n; ++i) {
currentgcd = gcd(currentgcd, health[i]);
}
return currentgcd;
}
int main()
{
int health[] = { 4, 6, 8, 12 };
int n = sizeof (health) / sizeof (health[0]);
cout << solve(health, n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int gcd( int a, int b)
{
if (a == 0 )
return b;
return gcd(b % a, a);
}
static int solve( int health[], int n)
{
int currentgcd = gcd(health[ 0 ], health[ 1 ]);
for ( int i = 2 ; i < n; ++i)
{
currentgcd = gcd(currentgcd, health[i]);
}
return currentgcd;
}
public static void main(String args[])
{
int health[] = { 4 , 6 , 8 , 12 };
int n = health.length;
System.out.println(solve(health, n));
}
}
|
Python3
def gcd(a, b):
if (a = = 0 ):
return b
return gcd(b % a, a)
def solve(health, n):
currentgcd = gcd(health[ 0 ], health[ 1 ])
for i in range ( 2 , n):
currentgcd = gcd(currentgcd,
health[i])
return currentgcd
health = [ 4 , 6 , 8 , 12 ]
n = len (health)
print (solve(health, n))
|
C#
using System;
class GFG
{
static int gcd( int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static int solve( int []health, int n)
{
int currentgcd = gcd(health[0], health[1]);
for ( int i = 2; i < n; ++i)
{
currentgcd = gcd(currentgcd, health[i]);
}
return currentgcd;
}
public static void Main(String []args)
{
int []health = { 4, 6, 8, 12 };
int n = health.Length;
Console.WriteLine(solve(health, n));
}
}
|
Javascript
<script>
function gcd(a, b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
function solve(health, n)
{
let currentgcd = gcd(health[0], health[1]);
for (let i = 2; i < n; ++i)
{
currentgcd = gcd(currentgcd, health[i]);
}
return currentgcd;
}
let health = [ 4, 6, 8, 12 ];
let n = health.length;
document.write(solve(health, n));
</script>
|
PHP
<?php
function gcd( $a , $b )
{
if ( $a == 0)
return $b ;
return gcd( $b % $a , $a );
}
function solve( $health , $n )
{
$currentgcd = gcd( $health [0],
$health [1]);
for ( $i = 2; $i < $n ; ++ $i )
{
$currentgcd = gcd( $currentgcd ,
$health [ $i ]);
}
return $currentgcd ;
}
$health = array (4, 6, 8, 12);
$n = sizeof( $health );
echo solve( $health , $n );
?>
|
Time Complexity: O(N * log(MAX)) where N is the size of the array and MAX is the maximum number in the array.
We are running a loop that takes O(N) time. Also, the GCD function takes O(log(min(A, B)), and in the worst case when A and B are the same and A = B = MAX then the GCD function takes O(log(MAX)) time. So, overall time complexity = O(N * log(MAX))
Auxiliary Space: O(log(MAX))