# Concentration of juice after mixing n glasses in equal proportion

Given an array **arr[]** where **arr[i]** is the concentration of juice in **i ^{th}** 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 <bits/stdc++.h> ` `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[0]); ` ` ` `cout << mixtureConcentration(n, arr); ` `} ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of the approach ` ` ` `// Function to return the concentration ` `// of the resultant mixture ` `function` `mixtureConcentration(` `$n` `, ` `$p` `) ` `{ ` ` ` `$res` `= 0; ` ` ` `for` `(` `$i` `= 0; ` `$i` `< ` `$n` `; ` `$i` `++) ` ` ` `$res` `+= ` `$p` `[` `$i` `]; ` ` ` `$res` `/= ` `$n` `; ` ` ` `return` `$res` `; ` `} ` ` ` `// Driver code ` `$arr` `= ` `array` `( 0, 20, 20 ); ` `$n` `= ` `count` `(` `$arr` `); ` `print` `(` `round` `(mixtureConcentration(` `$n` `, ` `$arr` `), 4)); ` ` ` `// This code is contributed by chandan_jnu ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

13.3333

**Time Complexity:** O(N)

## Recommended Posts:

- Minimum number of bottles required to fill K glasses
- Check whether the given integers a, b, c and d are in proportion
- Minimum value of N such that xor from 1 to N is equal to K
- Count numbers whose XOR with N is equal to OR with N
- Number of Quadruples with GCD equal to K
- Find Multiples of 2 or 3 or 5 less than or equal to N
- Count of Multiples of A ,B or C less than or equal to N
- Sum of digits equal to a given number in PL/SQL
- Maximum Primes whose sum is equal to given N
- Pairs with GCD equal to one in the given range
- Number of subarrays with GCD equal to 1
- Check whether a subsequence exists with sum equal to k if arr[i]> 2*arr[i-1]
- Count pairs with set bits sum equal to K
- Sum of Semi-Prime Numbers less than or equal to N
- Count pairs from two arrays having sum equal to K

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.