Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Heaps: Gladiator Strength In a fighting tournament, every gladiator fights with another gladiator, and every gladiator has a specified strength. When two gladiators with strengths

Heaps: Gladiator Strength In a fighting tournament, every gladiator fights with another gladiator, and every gladiator has a specified strength. When two gladiators with strengths a & b fight (a <= b), there are 2 possibilities: 1. If a = b, both are eliminated. 2. Else a is eliminated and strength of b is reduced to b-a. In every fight, two gladiators with maximum strength clash. After all the rounds, only one gladiator wins. Find minimum strength that the winning gladiator can have. Print 0 if no gladiator is left after the final fight. Function Description In the provided code snippet, implement the provided lastGladiatorStrength (...) method using the variables to print the minimum final strength of the winning gladiator. You can write your code in the space below the phrase "WRITE YOUR LOGIC HERE". There will be multiple test cases running so the Input and Output should match exactly as provided. The base Output variable result is set to a default value of -404 which can be modified. Additionally, you can add or remove these output variables. Input Format The first line contains an integer N denoting the total number of gladiators. The second line contains N space-separated integers A a... an-1-rSample Input 5 29418 -- denotes the total number of gladiators N denotes the strengths of N gladiators Constraints 1 <= N <= 30 1 <= Ai <= 1000. Output Format The output contains an integer denoting the minimum strength that the winning gladiator can have. If no gladiator is left, print 0. Sample Output 0 Explanation 1st fight is between gladiators with strengths 9 & 8. After the fight, the gladiator with strength 8 is eliminated and the opponent's strength is reduced to 1. The array becomes [2,1,4,1,0]. 2nd fight is between gladiators with strengths 4 & 2. After the fight, the gladiator with strength 2 is eliminated and the opponent's strength is reduced to 2. The array becomes [0,1,2,1,0]. 3rd fight is between gladiators with strengths 2 & 1. After the fight, the gladiator with strength 1 is eliminated and the opponent's strength is reduced to 1. The array becomes [0,0,1,1,0]. 4th fight, which is the last fight, is between gladiators with strengths 1 & 1. Both are eliminated. The array becomes [0,0,0,0,0]. Since there are no gladiators left, the output is 0.r1 import java.util.*; 2 import java.io.*; 3 import java.lang.Math; 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 public class Main{ public static int lastGladiatorStrength(int n, int[] A) { //this is default OUTPUT. You can change it. int result = -404; //write your Logic here: } } return result; public static void main (String[] args) { Scanner sc = new Scanner(System.in); // INPUT [uncomment & modify if required] int N = sc.nextInt (); int[] A = new int [N]; for (int i = 0; i < N; i++) { A[i] = sc.nextInt (); } } sc.close(); System.out.print(lastGladiatorStrength (N, A));

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Design Operation And Evaluation Of Mobile Communications

Authors: Gavriel Salvendy ,June Wei

1st Edition

3030770249, 978-3030770242

More Books

Students also viewed these Programming questions

Question

Define and explain the Bioptic Telescope

Answered: 1 week ago