Problem 200B Drinks - Java Solution and Explanation
Problem Overview:
Vasy has n different drinks. Each drink contains a certain percentage of orange juice.
He mixes the same amount of every drink to create one cocktail.
For example, if the drinks contain:
50 50 100
the resulting percentage is
(50+50+100)/3=66.666666....
Thus, the answer is approximately:
66.66666666667
Understanding the problem:
The important observation is that Vasya uses an equal amount of every drink.
Because every drink contributes a small amount to the cocktail.
So, the percentage of orange juice in the final cocktail is simply the average of all the given percentages.
So, if the percentages are:
p1, p2, p3, ..., pn
the answer is
(p1 + p2 + p3 + ... + pn) / n
We don't need to simulate the actual mixing process.
Approcah
- Read the number of drinks
- Add the percentage of orange juice from every drink.
- Divide the total by the number of drinks.
Step-by-Step Explanation
Java Solution
import java.util.Scanner;
public class Main{
public static void main(String main[]){
Scanner sc = new Scanner(System.in);
int inputs=sc.nextInt();
int sum=0;
for(int pos =0;pos<inputs;pos++){
sum += sc.nextInt();
}
double answer = (double) sum / inputs;
System.out.println(answer);
sc.close();
}
}
Why do we use double?
This is an important detail in Java.
If we write:
System.out.println(sum / n);
both sum and n are
integers. Java therefore performs integer division.
For example:
150 / 4
would produce:
37
instead of:
37.5
To obtain the decimal result, we convert one of the values to
double:
double answer = (double) sum / n;
Now Java performs floating-point division:
150.0 / 4 = 37.5
This is necessary because Codeforces accepts answers with a small floating-point error.
Example Walkthrough
Example 1
Input:
3
50 50 100
The sum is:
50 + 50 + 100 = 200
There are 3 drinks:
200 / 3 = 66.666666...
Output:
66.66666666666667
The exact number of decimal places is not important because the problem accepts a small numerical error.
Example 2
Input:
4
0 25 50 75
Sum:
0 + 25 + 50 + 75 = 150
Average:
150 / 4 = 37.5
Output:
37.5
Complexity Analysis
The program reads each drink percentage exactly once.
Time Complexity
O(n)
where n is the number of drinks.
Space Complexity
O(1)
We only store the number of drinks and their running sum. We don't need to store the entire input array.
Common Mistakes
1. Using integer division
Avoid:
System.out.println(sum / n);
because both operands are integers.
Use:
System.out.println((double) sum / n);
instead.
2. Forgetting to divide by the number of drinks
The required answer is the average, not the total percentage.
Incorrect:
System.out.println(sum);
Correct:
System.out.println((double) sum / n);
3. Overcomplicating the solution
There is no need to simulate the volume of each drink.
Since equal amounts of all drinks are mixed, calculating their average percentage gives the required answer directly.
Key Takeaway
The main idea behind the problem is mixing equal quantities means the final concentration is the arithmetic mean of the individual concentrations.
The solution therefore requires only:
sum all percentages
↓
divide by number of drinks
↓
print the floating-point result
This problem is a useful reminder that Java's integer division can cause incorrect results when a decimal answer is required.
0 Comments
If you have any doubts or any topics that you want to know more about them please let me know