Ticker

6/recent/ticker-posts

Header Ads Widget

Responsive Advertisement

Codeforecess 200B - Drinks: Java Solution and Explanation

Problem 200B Drinks - Java Solution and Explanation

Problem 200B - Drinks: This is a extemely simple problem based on calculating the average percentage of orange juice in several drinks.

The problem is a good introduction to averages, loops, and decimal arithmetic in Java.


    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

    We can solve the problem in three simple steps:
    1. Read the number of drinks
    2. Add the percentage of orange juice from every drink.
    3. Divide the total by the number of drinks.
    The result must be calculated using floating-point division because the answer may contain a decimal part.

    Step-by-Step Explanation

    Suppose the input is:
    4
    0 25 50 75

    Step 1: Add all percentages
    0 + 25 + 50 + 75 = 150

    Step 2: Divide by the number of drinks
    There are 4 drinks:
    150 /4 = 37.5

    Therefore, the cocktail contains:
    37.5%
    orange juice.

    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.

    Post a Comment

    0 Comments