Question

Write the Java(Java 7 or Java 8) program for this problem:- Thanos, in his mission to...

Write the Java(Java 7 or Java 8) program for this problem:-

Thanos, in his mission to restore the ecological balance in the universe, has reached planet earth. He considers a planet ecologically balanced if more than half of the people on the planet have the same Consumption Capacity

There are N people on planet earth, each having Consumption Capacity C1, C2, ...CN and Strength S1, S2... Sn . Thanos will make earth ecological balanced by killing some people(Possibly None). To kill a person with Strength K, Thanos spends K units of his energy.

Thanos, being a conservationist, wants to spend as less energy as possible in the process. Help him determine the minimum energy he should spend to make earth ecologically balanced.

Input:

• First line of input contains a single integer N denoting the number of people on planet Earth

• The second line contains N space separated integers C1,C2.... CN. denoting the Consumption Capacity of the ith person.

• The third line contains N space separated integers S1, S2, ... SN denoting the Strength of the ith person.

Output:

Output a single integer denoting the minimum energy Thanos spends.

Constraints

•1<=N<=2* 10^5

• 1< =Ci <= 10^6

• 1<=Si, <= 10^9

Sample Input 1:

1

5

10

Sample Output 1:

0

EXPLANATION:

Thanos won't kill anyone as the planet is already ecologically balanced. Hence energy spent = 0

Sample Input 2:

2

5 8

4 2

Sample Output 2:

2

EXPLANATION:

Thanos will kill the person with Strength = 2 making earth ecologically balanced

Sample Input 3:

7
2 2 1 1 1 3 3
20 30 2 5 3 15 3

Sample Output 3:

13

EXPLANATION:

Thanos will kill 3rd 4th 5th and 7th person making earth ecologically balanced and spending the least energy = 13.

Homework Answers

Answer #1

CODE:

import java.util.*;
class Thanos
{
   public static void main(String[] args)
   {
       Scanner in=new Scanner(System.in);//scanner object.
       int n=in.nextInt();//taking input of n from user.
       int c[]=new int[n];//Array for consumption capacity.
       int s[]=new int[n];//Array for strengths.
       int i;//loop variable.
       //loop to take consumption capacities from user.
       for(i=0;i<n;i++)
       {
           //taking input of comsumption capacity.
           c[i]=in.nextInt();
       }
       //loop to take the strengths from user.
       for(i=0;i<n;i++)
       {
           //taking input of strngths.
           s[i]=in.nextInt();
       }
       Arrays.sort(s);//we sort the strengths.
       int min=0;//The minimum energy of Thanos variable.
       //if people are only 1 then there is no need to kill.
       if(n==1)
       {
           System.out.println(min);//prints 0.
       }
       //if people are even.
       else if(n%2==0)
       {
           //we run loop till half and strenghts required
           for(i=0;i<n/2;i++)
           {
               //adding the strengths to min.
               min=min+s[i];
           }
           System.out.println(min);//printing the min.
       }
       //if people are odd. we kill half +1 people.
   else
   {
       //loop till 1 more than half of the people.
           for(i=0;i<=n/2;i++)
           {
               //adding the min.
               min=min+s[i];
           }
           System.out.println(min);//printing min.
   }
   }
}

CODE ATTACHMENTS:

OUTPUTS:

I have sorted so that minimum strengths will be at first half.

Then sum the first half strengths.

Then printed the sum.

Please do comment for any queries.
PLease like it.
Thank You.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
* Write a Java program that calculates and displays the Fibonacci numbers * First prompt the...
* Write a Java program that calculates and displays the Fibonacci numbers * First prompt the user to input a number, N * Then use a for loop to calculate and display the first N fibonocci numbers * For example, if the user enters 5, the output should be: * 1, 1, 2, 3, 5 * * if the user enters 10, the output should be: * 1, 1, 2, 3, 5, 8, 13, 21, 34, 55
Please code in Java and please implement constarints Digital Root and Iterations Given a non-negative integer,...
Please code in Java and please implement constarints Digital Root and Iterations Given a non-negative integer, print out its digital root and the number of iterations required to reach it. The digital root is the single digit number obtained by an iterative process of finding the sum of digits. In the next iteration, the sum of the digits in the previous iteration is computed, and the process repeated until a single digit value is obtained. Input Format The first line...
Java Write the method rightN. The method has two inputs: a String str and an int...
Java Write the method rightN. The method has two inputs: a String str and an int n. The output of the method is a new String that contains a "rotated right n" version of str. You can assume that the length of str will be at least n. Here is some sample input and output: rightN("Hello", 2) → "loHel" rightN("Chocolate", 3) → "ateChocol " rightN("Chocolate", 1) → "eChocolat"
In Java. Write a program that reads-in a times table-number. The program, using this table-number will...
In Java. Write a program that reads-in a times table-number. The program, using this table-number will produce the following report (as shown). The first item in the report is a number with starting value 1. Second column is the word “ X ” (representing the times symbol). Third column is the table-number (itself). Following is an equal sign “ = “ (representing a result). Last column is the result of the operation for that line or row which is the...
You are required to write a program in JAVA based on the problem description given. Read...
You are required to write a program in JAVA based on the problem description given. Read the problem description and write a complete program with necessary useful comment for good documentation. Compile and execute the program. ASSIGNMENT OBJECTIVES: • To introduce queue data structure. DESCRIPTIONS OF PROBLEM: Exercise : Write a program to reverse element of a stack. For any given word (from input), insert every character (from the word) into a stack. The output from the stack should be...
4) Write a Java program using Conditions: Write a program where it will ask user to...
4) Write a Java program using Conditions: Write a program where it will ask user to enter a number and after that it will give you answer how many digits that number has. Steps: 1) Create Scanner object and prompt user to enter the number and declare variable integer for input 2) Use if else condition with && condition where you will specify the digits of numbers by writing yourself the digit number that should display: Example(num<100 && num>=1), and...
February 18 is a special date for the CCC this year. Write a program that asks...
February 18 is a special date for the CCC this year. Write a program that asks the user for a numerical month and numerical day of the month and then determines whether that date occurs before, after, or on February 18. If the date occurs before February 18, output the word Before. If the date occurs after February 18, output the word After. If the date is February 18, output the word Special. Input The input consists of two integers...
Write a Java program to display a letter grade based on an actual grade. A =...
Write a Java program to display a letter grade based on an actual grade. A = 90-100 B = 80-89 C = 70-79 D = 60-69 F = less than 60 The program needs to do the following: 1. Create a short integer to store a grade. (See Week 3 - Things to Know - Java Data Types ) 2. Prompt for a grade ( 0-100). 3. Based on the grade given, use IF statements to display the letter grade...
Write a complete recursive java program to compute the heights of the tick marks on a...
Write a complete recursive java program to compute the heights of the tick marks on a ruler. Assume that the length of the ruler is a power of 2 (say L=2^n, where n >=1) , and the marks are to be placed at every point between 0 and 2^n, not including the endpoints. The endpoints 0 and 2^n will have height 0. Here are the rules for computing the heights of the ticks for a ruler of length L=2^n: 1....
This is a Java program Program Description You work for a local cell phone company and...
This is a Java program Program Description You work for a local cell phone company and have been asked to write a program to calculate the price of a cell phone data plan being purchased by a customer. The program should do the following tasks: Display a menu of the data plans that are available to be purchased. Read in the user’s selection from the menu.  Input Validation: If the user enters an invalid option, the program should display an error...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT