Find the closest Fraction to given fraction having minimum absolute difference
Last Updated :
27 Jul, 2021
Given three integers x, y, and n, the task is to find such a pair of integers a, ?b (1?<=?b?<=?n; 0?<=?a) that the value |x/y – a/b| is as minimal as possible, where |x| represents the absolute value of x.
Examples:
Input: x = 3, y = 7, n = 6
Output: 2/5
Explanation: a=2 and b=5 and b<=6 then 3/7 – 2/5 = 1/35, which is the minimum difference possible
Input: x = 12, y = 37, n = 5
Output: 1/3
Approach: Iterate over the denominator. Let the denominator be i. Then it is required to choose such a numerator d such that |x/y – d/i| is minimal. d = (x*i)/y is a good choice. Also check for d+1. While updating the answer from A/B to d/i, check if x/y – d/i < x/y -A/B or (B*x-y*A) * (i*y) > (i*x-y*d) * (B*y). Follow the steps below to solve the problem:
- Initialize the variables A and B as -1 to store the answers.
- Iterate over the range [1, N] using the variable i and perform the following steps:
- Initialize the variable d as (i*x)/y as the closest possible numerator.
- If d is greater than equal to 0 and A is equal to -1 or ABS(B * x – y * A) * ABS(i * y) is greater than ABS(i * x – y * d) * ABS(B * y), then set the value of A as d and B as i.
- Increase the value of d by 1.
- If d is greater than equal to 0 and A is equal to -1 or ABS(B * x – y * A) * ABS(i * y) is greater than ABS(i * x – y * d) * ABS(B * y), then set the value of A as d and B as i.
- After performing the above steps, print the value of A/B as the answer.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
long long ABS( long long x) { return max(x, -x); }
void findFraction( long long x, long long y, long long n)
{
long long A = -1, B = -1;
for ( long long i = 1; i <= n; i++) {
long long d = (i * x) / y;
if (d >= 0
&& (A == -1
|| ABS(B * x - y * A) * ABS(i * y)
> ABS(i * x - y * d) * ABS(B * y)))
A = d, B = i;
d++;
if (d >= 0
&& (A == -1
|| ABS(B * x - y * A) * ABS(i * y)
> ABS(i * x - y * d) * ABS(B * y)))
A = d, B = i;
}
cout << A << "/" << B << endl;
}
int main()
{
long long x = 3, y = 7, n = 6;
findFraction(x, y, n);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static long ABS( long x) { return Math.max(x, -x); }
static void findFraction( long x, long y, long n)
{
long A = - 1 , B = - 1 ;
for ( long i = 1 ; i <= n; i++) {
long d = (i * x) / y;
if (d >= 0
&& (A == - 1
|| ABS(B * x - y * A) * ABS(i * y)
> ABS(i * x - y * d)
* ABS(B * y)))
A = d;
B = i;
d++;
if (d >= 0
&& (A == - 1
|| ABS(B * x - y * A) * ABS(i * y)
> ABS(i * x - y * d)
* ABS(B * y)))
A = d;
B = i;
}
A--;
B--;
System.out.println(A + "/" + B);
}
public static void main(String[] args)
{
long x = 3 ;
long y = 7 ;
long n = 6 ;
findFraction(x, y, n);
}
}
|
Python3
def ABS (x):
return max (x, - x)
def findFraction(x, y, n):
A = - 1
B = - 1
for i in range ( 1 , n + 1 ):
d = (i * x) / / y
if (d > = 0 and (A = = - 1 or
ABS (B * x - y * A) * ABS (i * y) >
ABS (i * x - y * d) * ABS (B * y))):
A = d
B = i
d + = 1
if (d > = 0 and (A = = - 1 or
ABS (B * x - y * A) * ABS (i * y) >
ABS (i * x - y * d) * ABS (B * y))):
A = d
B = i
print ( str (A) + "/" + str (B))
if __name__ = = '__main__' :
x = 3
y = 7
n = 6
findFraction(x, y, n)
|
C#
using System;
public class GFG{
static long ABS( long x) { return Math.Max(x, -x); }
static void findFraction( long x, long y, long n)
{
long A = -1, B = -1;
for ( long i = 1; i <= n; i++) {
long d = (i * x) / y;
if (d >= 0
&& (A == -1
|| ABS(B * x - y * A) * ABS(i * y)
> ABS(i * x - y * d)
* ABS(B * y)))
A = d;
B = i;
d++;
if (d >= 0
&& (A == -1
|| ABS(B * x - y * A) * ABS(i * y)
> ABS(i * x - y * d)
* ABS(B * y)))
A = d;
B = i;
}
A--;
B--;
Console.Write(A + "/" + B);
}
static public void Main (){
long x = 3;
long y = 7;
long n = 6;
findFraction(x, y, n);
}
}
|
Javascript
<script>
function ABS(x)
{
return Math.max(x, -x);
}
function findFraction(x, y, n)
{
let A = -1, B = -1;
for (let i = 1; i <= n; i++)
{
let d = Math.floor((i * x) / y);
if (d >= 0 && (A == -1 || ABS(B * x - y * A) *
ABS(i * y) > ABS(i * x - y * d) * ABS(B * y)))
A = d;
B = i;
d++;
if (d >= 0 && (A == -1 || ABS(B * x - y * A) *
ABS(i * y) > ABS(i * x - y * d) * ABS(B * y)))
A = d;
B = i;
}
A--;
B--;
document.write(A + "/" + B);
}
let x = 3;
let y = 7;
let n = 6;
findFraction(x, y, n);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...