Slime Rancher

You are playing a game titled Slime Rancher 2. You will be training slimes in this game. You have a slime-in-training. Associated with the slime are N attributes, numbered 0 through N-1, each represented by a positive integer. You are given int[] attributes containing N integers : the i-th integer is the initial value of the i-th attribute for the slime. After the training is complete, each of the slime’s attributes will either stay the same or increase to some positive integer less than or equal to 999. None of the attributes will decrease in value. The weight of the training is defined as the sum of the differences between the final and initial values of all the attributes for the slime.
You are a master slime breeder, and you’re able to obtain any possible final values for a slime’s attributes. This time, you would like to create a well-balanced slime. A slime is well-balanced if all of its attributes have equal values. What is the minimum possible weight of the training?

Examples

1){1,2,3} = > 3

Train the slime such that all of its attributes are equal to 3. The total weight of the training is |3 – 1| + |3 – 2| + |3 – 3| = 3.

2){5,5} => 0

This slime is already well-balanced.

3){900,500,100} => 1200

import java.util.*;

public class SlimeXSlimeRancher2 {

  public int train(int[] attributes){
    int tvalue =0;
    Arrays.sort(attributes);
    int max = attributes[attributes.length-1];
    
    for (int i=0;i<attributes.length;i++){
      tvalue +=max -attributes[i];
    }
    System.out.println(tvalue);
    return tvalue;
  }

  public static void  main(String args[]) {
      SlimeXSlimeRancher2 ss = new SlimeXSlimeRancher2();
      ss.train(new int[] {1, 2, 3});
      ss.train(new int[]     {5, 5});
      ss.train(new int[]     {900, 500, 100});
      ss.train(new int[]     {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 999});
      ss.train(new int[]    {1, 1, 1, 1, 1});
  }
}
Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: