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
Java Program Sequential Search You are given a sequence of n integers S and a sequence...
Java Program Sequential Search You are given a sequence of n integers S and a sequence of different q integers T. Write a program which outputs C, the number of integers in T which are also in the set S. Input: In the first line n is given. In the second line, n integers are given. In the third line q is given. Then, in the fourth line, q integers are given. Output: Print C in a line. Constraints n...
* 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...
Intro to JAVA Problem 1: Summing It Up Write a program, which takes two distinct integers...
Intro to JAVA Problem 1: Summing It Up Write a program, which takes two distinct integers separated by space as input and prints the sum of all the integers between them, including the two given numbers. Note that the numbers can appear in either order. You may assume that both numbers are between –10, 000 and 10, 000. For example, if the input is as follows: 10 4 the output should be 49, since 10+9+8+7+6+5+4=49. Similarly, if the input is...
JAVA Problem 1: Summing It Up Write a program, which takes two distinct integers separated by...
JAVA Problem 1: Summing It Up Write a program, which takes two distinct integers separated by space as input and prints the sum of all the integers between them, including the two given numbers. Note that the numbers can appear in either order. You may assume that both numbers are between –10, 000 and 10, 000. For example, if the input is as follows: 10 4 the output should be 49, since 10+9+8+7+6+5+4=49. Similarly, if the input is -3 10...
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"
Write a JAVA program that reads in a string from standard input and determines the following:...
Write a JAVA program that reads in a string from standard input and determines the following: - How many vowels are in the string (FOR THE PURPOSE OF THIS PROGRAM 'Y' is NOT considered a vowel)? - How many upper case characters are in the string? - How many digits are in the string? - How many white space characters are in the string? - Modify the program to indicate which vowel occurs the most. In the case of a...
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...
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...
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...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT