Consider the set of irreducible fractions A = {n/d | n≤d and d ≤ 10000 and gcd(n, d) = 1}. You are given a member of this set and your task is to find the largest fraction in this set less than the given fraction.
Note: This is a set and all the members are unique.
Examples:
Input: n = 1, d = 4
Output: {2499, 9997}
Explanation: 2499/9997 is the largest fraction.
Input: n = 2, d = 4
Output: {4999, 9999}
Explanation: 4999/9999 is the largest fraction.
Approach: The solution to the problem is based on the following mathematical concept:
Say the desired fraction is p/q. So,
p/q < n/d
p < (n*q)/d
As we want p/q to be smaller than n/d, start to iterate over q from q = d+1.
Then for each value of q, the value of p will be floor((n*q)/d).
Follow the below steps to implement the above idea:
- Create two variables num and den to store the final answer. Initialize them as num =- 1 and den =1.
- Now, loop i from d+1 to 10000:
- Calculate the value of the fraction with denominator as i using the above observation.
- The numerator will be (n*i)/d [this is integer division here, i.e., it gives the floor value] and denominator = i+1
- If the fraction is greater than num/den, then update num and den accordingly.
- After all the iterations num and den will store the required numerator and denominator.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
vector< int > numAndDen( int n, int d)
{
int num = -1, den = 1;
vector< int > ans;
for ( int i = d + 1; i <= 10000; i++) {
int x = (n * i) / d;
if (1.0 * x / i > 1.0 * num / den
and __gcd(x, i) == 1)
num = x, den = i;
}
ans.push_back(num);
ans.push_back(den);
return ans;
}
int main()
{
int n = 1, d = 4;
vector< int > ans = numAndDen(n, d);
for ( auto i : ans)
cout << i << " " ;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static int gcd( int a, int b)
{
if (b == 0 )
return a;
return gcd(b, a % b);
}
public static ArrayList<Integer> numAndDen( int n, int d)
{
int num = - 1 ;
int den = 1 ;
ArrayList<Integer> ans = new ArrayList<Integer>();
for ( int i = d + 1 ; i <= 10000 ; i++) {
int x = (n * i) / d;
if ( 1.0 * x / i > 1.0 * num / den
&& gcd(x, i) == 1 ) {
num = x;
den = i;
}
}
ans.add(num);
ans.add(den);
return ans;
}
public static void main(String[] args)
{
int n = 1 , d = 4 ;
ArrayList<Integer> ans = numAndDen(n, d);
for (Integer i : ans)
System.out.print(i + " " );
}
}
|
Python3
from math import gcd
def numAndDen(n, d) :
num = - 1 ; den = 1 ;
ans = [];
for i in range (d + 1 , 10001 ) :
x = (n * i) / / d;
if (( 1.0 * x / i) > ( 1.0 * num / den) and gcd(x, i) = = 1 ) :
num = x;
den = i;
ans.append(num);
ans.append(den);
return ans;
if __name__ = = "__main__" :
n = 1 ; d = 4 ;
ans = numAndDen(n, d);
for i in ans:
print (i,end = " " );
|
C#
using System;
using System.Collections;
public class GFG{
static int gcd( int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static ArrayList numAndDen( int n, int d)
{
int num = -1;
int den = 1;
ArrayList ans = new ArrayList();
for ( int i = d + 1; i <= 10000; i++) {
int x = (n * i) / d;
if (1.0 * x / i > 1.0 * num / den
&& gcd(x, i) == 1) {
num = x;
den = i;
}
}
ans.Add(num);
ans.Add(den);
return ans;
}
static public void Main (){
int n = 1, d = 4;
ArrayList ans = numAndDen(n, d);
foreach ( var i in ans)
Console.Write(i + " " );
}
}
|
Javascript
<script>
function __gcd(a, b) {
if (b == 0)
return a;
return __gcd(b, a % b);
}
function numAndDen(n, d) {
let num = -1, den = 1;
let ans = [];
for (let i = d + 1; i <= 10000; i++) {
let x = Math.floor((n * i) / d);
if (1.0 * (x / i) > 1.0 * (num / den)
&& __gcd(x, i) == 1)
num = x, den = i;
}
ans.push(num);
ans.push(den);
return ans;
}
let n = 1, d = 4;
let ans = numAndDen(n, d);
for (let i of ans)
document.write(i + " " );
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)