Thursday, December 11, 2025
HomeData Modelling & AIFind the ratio of number of elements in two Arrays from their...

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 (a*x + b*y)    .
  • Total number of elements in the combined array is (x + y)    and let f = x / y    .
  • So, (a*x + b*y) / (x + y) = c
    (a*f + b) / (f + 1) = c
    f * (c - a) = b - c
    So, f = (b - c) / (c - a)
  • 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;
}


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


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 = 4
b = 10
c = 6
 
FindRatio(a, b, c)
 
# This code is contributed by Mohit Kumar


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


Javascript




<script>
// Javascript program to Find the Ratio
// of number of Elements in two Arrays
// from their individual and combined Average
 
// Javascript function to find the ratio
// of number of array elements
function FindRatio(a, b, c)
{
 
    let up = Math.abs(b - c);
    let down = Math.abs(c - a);
 
    // calculating GCD of them
    let g = gcd(up, down);
 
    // make neumarator and
    // denominator coprime
    up = parseInt(up / g);
    down = parseInt(down / g);
 
    document.write(up + ":"
         + down + "<br>");
}
 
function gcd(a, b)
{
  if (b == 0)
      return a;
  return gcd(b, a % b);
 
}
 
// Driver Code
 
    let a = 4, b = 10, c = 6;
 
    FindRatio(a, b, c);
 
</script>


Output: 

2:1

 

Time Complexity: O(log( min(abs(b-c),abs(c-a)) ) )

Auxiliary Space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Dominic
32441 POSTS0 COMMENTS
Milvus
105 POSTS0 COMMENTS
Nango Kala
6813 POSTS0 COMMENTS
Nicole Veronica
11950 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12024 POSTS0 COMMENTS
Shaida Kate Naidoo
6943 POSTS0 COMMENTS
Ted Musemwa
7195 POSTS0 COMMENTS
Thapelo Manthata
6889 POSTS0 COMMENTS
Umr Jansen
6880 POSTS0 COMMENTS