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.
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.
Get Answers For Free
Most questions answered within 1 hours.