Related Articles

# Concentration of juice after mixing n glasses in equal proportion

• Last Updated : 27 Apr, 2021

Given an array arr[] where arr[i] is the concentration of juice in ith glass. The task is to find the concentration of the resultant mixture when all the glasses are mixed in equal proportions.

Examples:

Input: arr[] = {10, 20, 30}
Output: 20
Input: arr[] = {0, 20, 20}
Output: 13.3333

Approach: Since the juices are mixed in equal proportions so the resultant concentration will be the average of all the individual concentrations. Therefore the required answer would be sum(arr) / n where n is the size of the array.
Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach``#include ``using` `namespace` `std;` `// Function to return the concentration``// of the resultant mixture``double` `mixtureConcentration(``int` `n, ``int` `p[])``{``    ``double` `res = 0;``    ``for` `(``int` `i = 0; i < n; i++)``        ``res += p[i];``    ``res /= n;``    ``return` `res;``}` `// Driver code``int` `main()``{``    ``int` `arr[] = { 0, 20, 20 };``    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr);``    ``cout << mixtureConcentration(n, arr);``}`

## Java

 `// Java implementation of the approach` `class` `GFG``{``    ` `// Function to return the concentration``// of the resultant mixture``static` `double` `mixtureConcentration(``int` `n, ``int` `[]p)``{``    ``double` `res = ``0``;``    ``for` `(``int` `i = ``0``; i < n; i++)``        ``res += p[i];``    ``res /= n;``    ``return` `res;``}` `// Driver code``public` `static` `void` `main (String[] args)``{` `    ``int` `[]arr = { ``0``, ``20``, ``20` `};``    ``int` `n = arr.length;``    ``System.out.println(String.format(``"%.4f"``,``                        ``mixtureConcentration(n, arr)));``}``}` `// This code is contributed by chandan_jnu`

## Python3

 `# Python3 implementation of the approach``    ` `# Function to return the concentration``# of the resultant mixture``def` `mixtureConcentration(n, p):` `    ``res ``=` `0``;``    ``for` `i ``in` `range``(n):``        ``res ``+``=` `p[i];``    ``res ``/``=` `n;``    ``return` `res;` `# Driver code``arr ``=` `[ ``0``, ``20``, ``20` `];``n ``=` `len``(arr);``print``(``round``(mixtureConcentration(n, arr), ``4``));` `# This code is contributed``# by chandan_jnu`

## C#

 `// C# implementation of the approach``using` `System;` `class` `GFG``{``    ` `// Function to return the concentration``// of the resultant mixture``static` `double` `mixtureConcentration(``int` `n, ``int` `[]p)``{``    ``double` `res = 0;``    ``for` `(``int` `i = 0; i < n; i++)``        ``res += p[i];``    ``res /= n;``    ``return` `Math.Round(res,4);``}` `// Driver code``static` `void` `Main()``{``    ``int` `[]arr = { 0, 20, 20 };``    ``int` `n = arr.Length;``    ``Console.WriteLine(mixtureConcentration(n, arr));``}``}` `// This code is contributed by chandan_jnu`

## PHP

 ``

## Javascript

 ``
Output:

`13.3333`

Time Complexity: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up