Write a program to find the ratio in which a shopkeeper will mix two types of rice worth Rs.
kg and Rs.
kg, so that the average cost of the mixture is Rs.
kg.
Examples:
Input: X = 50, Y = 70, Z = 65
Output: Ratio = 1:3
Input: X = 1000, Y = 2000, Z = 1400
Output: Ratio = 3:2
According to Alligation rule, the ratio of the weights of two items mixed will be inversely proportional to the deviation of attributes of these two items from the average attribute of the resultant mixture.
w1 / w2 = (d - m) / (m - c)

Below program illustrate the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void alligation( float x, float y, float m)
{
float c = (x <= y) ? x : y;
float d = (x >= y) ? x : y;
int r1 = d - m;
int r2 = m - c;
int gcd = __gcd(r1, r2);
cout << r1 / gcd << ":" << r2 / gcd;
}
int main()
{
float x, y, z;
x = 50;
y = 70;
z = 65;
alligation(x, y, z);
return 0;
}
|
Java
import java.util.*;
class solution
{
static float __gcd( float a, float b)
{
float dividend,divisor;
if (a>=b)
dividend = a;
else
dividend = b;
if (a<=b)
divisor = a;
else
divisor = b;
while (divisor> 0 )
{
float remainder = dividend % divisor;
dividend = divisor;
divisor = remainder;
}
return dividend;
}
static void alligation( float x, float y, float m)
{
float c;
if (x <= y)
c = x;
else
c = y;
float d ;
if (x >= y)
d = x;
else
d = y;
float r1 = d - m;
float r2 = m - c;
float gcd = __gcd(r1, r2);
System.out.println(( int )(r1 / gcd)+ ":" +( int )(r2 / gcd));
}
public static void main(String args[])
{
float x, y, z;
x = 50 ;
y = 70 ;
z = 65 ;
alligation(x, y, z);
}
}
|
Python3
from math import gcd
def alligation(x, y, m):
if (x < = y):
c = x
else :
c = y
if (x > = y):
d = x
else :
d = y
r1 = d - m
r2 = m - c
__gcd = gcd(r1, r2)
print (r1 / / __gcd, ":" , r2 / / __gcd)
if __name__ = = '__main__' :
x = 50
y = 70
z = 65
alligation(x, y, z)
|
C#
using System;
class GFG
{
static int __gcd( int a, int b)
{
if (a == 0)
return b;
if (b == 0)
return a;
if (a == b)
return a;
if (a > b)
return __gcd(a - b, b);
return __gcd(a, b - a);
}
static void alligation( float x,
float y, float m)
{
float c = (x <= y) ? x : y;
float d = (x >= y) ? x : y;
int r1 = ( int )(d - m);
int r2 = ( int )(m - c);
int gcd = __gcd(r1, r2);
Console.Write(r1 / gcd + ":" +
r2 / gcd);
}
public static void Main()
{
float x, y, z;
x = 50;
y = 70;
z = 65;
alligation(x, y, z);
}
}
|
PHP
<?php
function __gcd( $a , $b )
{
$dividend ; $divisor ;
if ( $a >= $b )
$dividend = $a ;
else
$dividend = $b ;
if ( $a <= $b )
$divisor = $a ;
else
$divisor = $b ;
while ( $divisor > 0)
{
$remainder = $dividend % $divisor ;
$dividend = $divisor ;
$divisor = $remainder ;
}
return $dividend ;
}
function alligation( $x , $y , $m )
{
if ( $x <= $y )
$c = $x ;
else
$c = $y ;
if ( $x >= $y )
$d = $x ;
else
$d = $y ;
$r1 = $d - $m ;
$r2 = $m - $c ;
$gcd = __gcd( $r1 , $r2 );
echo (int)( $r1 / $gcd ) . ":" .
(int)( $r2 / $gcd );
}
$x = 50;
$y = 70;
$z = 65;
alligation( $x , $y , $z );
?>
|
Javascript
<script>
function __gcd(a, b)
{
if (a == 0)
return b;
if (b == 0)
return a;
if (a == b)
return a;
if (a > b)
return __gcd(a - b, b);
return __gcd(a, b - a);
}
function alligation(x, y, m)
{
let c = (x <= y) ? x : y;
let d = (x >= y) ? x : y;
let r1 = (d - m);
let r2 = (m - c);
let gcd = __gcd(r1, r2);
document.write(parseInt(r1 / gcd, 10) + ":" + parseInt(r2 / gcd, 10));
}
let x, y, z;
x = 50;
y = 70;
z = 65;
alligation(x, y, z);
</script>
|
Time Complexity: O(log(min(x,y)), gcd function takes logarithmic time complexity.
Auxiliary Space: O(1) as constant space is being used.