Find the ratio of number of elements in two Arrays from their individual and combined average
Given the average of elements in two arrays as ‘a’ and ‘b’ respectively, and their combined average as ‘c’, the task is to find the ratio of the number of elements in two array.
Examples:
Input: a = 2, b = 8, c = 5 Output: 1:1 Input: a = 4, b = 10, c = 6 Output: 2:1
Approach:
- Let the number of elements in two arrays are respectively x and y.
- So sum of all elements in the combined array is
. - Total number of elements in the combined array is
and let
. - So,



So,
- Here f is our required answer.
Below is the implementation of the above Approach:
C++
// C++ program to Find the Ratio // of number of Elements in two Arrays // from their individual and combined Average #include <bits/stdc++.h> using namespace std; // C++ function to find the ratio // of number of array elements void FindRatio(int a, int b, int c) { int up = abs(b - c); int down = abs(c - a); // calculating GCD of them int g = __gcd(up, down); // make neumarator and // denominator coprime up /= g; down /= g; cout << up << ":" << down << "\n"; } // Driver Code int main() { int a = 4, b = 10, c = 6; FindRatio(a, b, c); return 0; } |
chevron_right
filter_none
Java
// Java program to Find the Ratio // of number of Elements in two Arrays // from their individual and combined Average class GFG { static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } // function to find the ratio // of number of array elements static void FindRatio(int a, int b, int c) { int up = Math.abs(b - c); int down = Math.abs(c - a); // calculating GCD of them int g = gcd(up, down); // make neumarator and // denominator coprime up /= g; down /= g; System.out.println(up + ":" + down); } // Driver Code public static void main (String[] args) { int a = 4, b = 10, c = 6; FindRatio(a, b, c); } } // This code is contributed by AnkitRai01 |
chevron_right
filter_none
Python3
# Python3 program to Find the Ratio # of number of Elements in two Arrays # from their individual and combined Average from math import gcd # function to find the ratio # of number of array elements def FindRatio(a, b, c): up = abs(b - c) down = abs(c - a) # calculating GCD of them g = gcd(up, down) # make neumarator and # denominator coprime up //= g down //= g print(up,":", down) # Driver Code a = 4b = 10c = 6 FindRatio(a, b, c) # This code is contributed by Mohit Kumar |
chevron_right
filter_none
C#
// C# program to Find the Ratio // of number of Elements in two Arrays // from their individual and combined Average using System; class GFG { static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } // function to find the ratio // of number of array elements static void FindRatio(int a, int b, int c) { int up = Math.Abs(b - c); int down = Math.Abs(c - a); // calculating GCD of them int g = gcd(up, down); // make neumarator and // denominator coprime up /= g; down /= g; Console.WriteLine(up + ":" + down); } // Driver Code public static void Main (String []args) { int a = 4, b = 10, c = 6; FindRatio(a, b, c); } } // This code is contributed by Arnab Kundu |
chevron_right
filter_none
Output:
2:1
Recommended Posts:
- Find the number which when added to the given ratio a : b, the ratio changes to c : d
- Count the number of sub-arrays such that the average of elements present in the sub-array is greater than that not present in the sub-array
- Sum of two numbers if the original ratio and new ratio obtained by adding a given number to each number is given
- Find the deleted value from the array when average of original elements is given
- Number of ways to choose elements from the array such that their average is K
- Count occurrences of the average of array elements with a given number
- Find nth Fibonacci number using Golden ratio
- Find combined mean and variance of two series
- Average of remaining elements after removing K largest and K smallest elements from array
- Find common elements in three sorted arrays
- Find a pair of elements swapping which makes sum of two arrays same
- intersection_update() in Python to find common elements in n arrays
- Number of arrays of size N whose elements are positive integers and sum is K
- Find the average of k digits from the beginning and l digits from the end of the given number
- Divide the array into minimum number of sub-arrays having unique elements
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.



