We are provided with a number N. Find the biggest proper fraction a/b such that a + b = N. Following are constraints for fraction.
- a/b is a proper fraction if a<b and a and b are coprimes i.e no common factor of a and b.
- There can be multiple proper fractions with sum of numerator and denominator equal to a given number. The main task is to find the fraction having the maximum floating point value.
Input : N = 3 Output : 1 2 Input : N = 12 Output : 5 7 Explanation: In the second example N = 12 Possible a and b's are: 1 11 5 7 But clearly 5/7 (=0.71..) is greater than 1/11 (=0.09..). Hence answer for N = 12 is 5 7.
The solution to this problem is more intuitive than algorithmic.
Consider the following points carefully to understand the formula presented later:
- A fraction has maximum value if numerator is as big as possible and the denominator is as small as possible.
- Here the constraints are the facts that numerator can’t be bigger than denominator and their sum should add up to N.
Keeping these two points in mind, we can get to the fact that answer to this problem will be ceil(n/2)-1 and floor(n/2)+1.
Now this solution will always work for odd N and all those even N whose (N/2) is even. This is due to the fact that these two cases will always generate coprimes with the above formula.
Now consider the following example:
N = 10
ceil(10/2)-1 = 4
floor(10/2)+1 = 6
Clearly 4 and 6 are the wrong answers as they are not coprimes. The correct answer is 3 and 7.
Hence for even N with odd (N/2) the formula becomes ceil(n/2)-2 and floor(n/2)+2.
This article is contributed by Vineet Joshi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find ΔX which is added to numerator and denominator both of fraction (a/b) to convert it to another fraction (c/d)
- Find the Nth digit in the proper fraction of two numbers
- Least Common Denominator (LCD)
- Print all proper fractions with denominators less than equal to N
- Sum of all proper divisors of a natural number
- Find N fractions that sum upto a given fraction N/D
- Sum of all proper divisors of natural numbers in an array
- Sum of product of proper divisors of all Numbers lying in range [L, R]
- Sum of all proper divisors from 1 to N
- Convert given Decimal number into an irreducible Fraction
- Product of proper divisors of a number for Q queries
- as_integer_ratio() in Python for reduced fraction of a given rational
- First occurrence of a digit in a given fraction
- Max count of unique ratio/fraction pairs in given arrays
- Convert decimal fraction to binary number
- Maximum rational number (or fraction) from an array
- Expressing a fraction as a natural number under modulo 'm'
- Greedy Algorithm for Egyptian Fraction
- Fraction module in Python