Thursday, January 9, 2025
Google search engine
HomeData Modelling & AILargest proper fraction with sum of numerator and denominator equal to a...

Largest proper fraction with sum of numerator and denominator equal to a given number

We are provided with a number N. Find the biggest proper fraction a/b such that a + b = N. Following are constraints for fraction. 
 

  1. a/b is a proper fraction if a<b and a and b are coprimes i.e no common factor of a and b.
  2. 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.

Examples: 
 

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.   

 

Recommended Practice

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. 
 

C++




// CPP program to find the largest fraction
// a/b such that a+b is equal to given number
// and a < b.
#include <iostream>
#include <cmath>
using namespace std;
  
void solve(int n)
{
    // Calculate N/2;
    float a = (float)n / 2;
  
    // Check if N is odd or even
    if (n % 2 != 0) 
  
        // If N is odd answer will be 
        // ceil(n/2)-1 and floor(n/2)+1
        cout << ceil(a) - 1 << " "
             << floor(a) + 1 << endl;
    else {
  
        // If N is even check if N/2 i.e a 
        // is even or odd
        if ((int)a % 2 == 0) {
  
            // If N/2 is even apply the
            // previous formula
            cout << ceil(a) - 1 << " " 
                 << floor(a) + 1 << endl;
        
  
        else {
          
            // If N/2 is odd answer will be 
            // ceil(N/2)-2 and floor(N/2)+2
            cout << ceil(a) - 2 << " " 
                 << floor(a) + 2 << endl;
        }
    }
}
  
// driver function
int main()
{
    int n = 34;
    solve(n);
    return 0;
}


Java




// Java program to find the 
// largest fraction a/b 
// such that a+b is equal 
// to given number and a < b.
  
class GFG
{
public static void solve(int n)
{
    // Calculate N/2;
    double a = n / 2;
      
    // Check if N is
    // odd or even
    if (n % 2 != 0
    {
        // If N is odd answer 
        // will be ceil(n/2)-1 
        // and floor(n/2)+1
        System.out.println((Math.ceil(a) - 1) + 
                         " " + (Math.floor(a) + 1));
    }
    else
    {
  
        // If N is even check
        // if N/2 i.e a 
        // is even or odd
        if ((int)(a) % 2 == 0
        {
  
            // If N/2 is even apply 
            // the previous formula
            System.out.println((Math.ceil(a) - 1) + 
                             " " + (Math.floor(a) + 1));
        
  
        else
        {
            // If N/2 is odd answer 
            // will be ceil(N/2)-2 
            // and floor(N/2)+2
            System.out.println((Math.ceil(a) - 2) + 
                             " " + (Math.floor(a) + 2));
        }
    }
}
  
// Driver code
public static void main(String[] args)
{
    int n = 34;
    solve(n);
}
}
  
// This code is contributed
// by mits


Python3




# Python3 program to find 
# the largest fraction a/b 
# such that a+b is equal to
# given number and a < b.
import math
  
def solve(n):
      
    # Calculate N/2;
    a = float(n / 2);
  
    # Check if N is odd or even
    if (n % 2 != 0): 
  
        # If N is odd answer 
        # will be ceil(n/2)-1 
        # and floor(n/2)+1
        print((math.ceil(a) - 1), 
              (math.floor(a) + 1));
    else:
  
        # If N is even check if N/2
        # i.e a is even or odd
        if (a % 2 == 0):
  
            # If N/2 is even apply
            # the previous formula
            print((math.ceil(a) - 1),
                  (math.floor(a) + 1));
          
        else:
              
            # If N/2 is odd answer
            # will be ceil(N/2)-2 
            # and floor(N/2)+2
            print((math.ceil(a) - 2),
                  (math.floor(a) + 2));
  
# Driver Code
n = 34;
solve(n);
      
# This code is contributed by mits


C#




// C# program to find the 
// largest fraction a/b 
// such that a+b is equal 
// to given number and a < b.
using System;
class GFG
{
public static void solve(int n)
{
    // Calculate N/2;
    double a = n / 2;
      
    // Check if N is
    // odd or even
    if (n % 2 != 0) 
    {
        // If N is odd answer 
        // will be ceil(n/2)-1 
        // and floor(n/2)+1
        Console.WriteLine((Math.Ceiling(a) - 1) + 
                           " " + (Math.Floor(a) + 1));
    }
    else 
    {
  
        // If N is even check
        // if N/2 i.e a 
        // is even or odd
        if ((int)(a) % 2 == 0) 
        {
  
            // If N/2 is even apply 
            // the previous formula
            Console.WriteLine((Math.Ceiling(a) - 1) + 
                               " " + (Math.Floor(a) + 1));
        
  
        else 
        {
            // If N/2 is odd answer 
            // will be ceil(N/2)-2 
            // and floor(N/2)+2
            Console.WriteLine((Math.Ceiling(a) - 2) + 
                               " " + (Math.Floor(a) + 2));
        }
    }
}
  
// Driver code
public static void Main()
{
    int n = 34;
    solve(n);
}
}
  
// This code is contributed
// by mits


PHP




<?php
// PHP program to find the largest
// fraction a/b such that a+b is
// equal to given number and a < b.
  
function solve($n)
{
      
    // Calculate N/2;
    $a = (float)$n / 2;
  
    // Check if N is odd or even
    if ($n % 2 != 0) 
  
        // If N is odd answer will
        // be ceil(n/2)-1 and
        // floor(n/2)+1
        echo ceil($a) - 1, " ",
            floor($a) + 1, "\n";
    else {
  
        // If N is even check if N/2
        // i.e a is even or odd
        if ($a % 2 == 0) {
  
            // If N/2 is even apply
            //  the previous formula
            echo ceil($a) - 1, " "
                floor($a) + 1, "\n";
        
  
        else {
          
            // If N/2 is odd answer
            // will be ceil(N/2)-2 
            // and floor(N/2)+2
            echo ceil($a) - 2, " ",
               floor($a) + 2, "\n";
        }
    }
}
  
// driver function
    $n = 34;
    solve($n);
      
// This code is contributed by ajit
?>


Javascript




<script>
  
    // Javascript program to find the 
    // largest fraction a/b 
    // such that a+b is equal 
    // to given number and a < b.
      
    function solve(n)
    {
        // Calculate N/2;
        let a = n / 2;
  
        // Check if N is
        // odd or even
        if (n % 2 != 0) 
        {
            // If N is odd answer 
            // will be ceil(n/2)-1 
            // and floor(n/2)+1
            document.write((Math.ceil(a) - 1) + 
                     " " + (Math.floor(a) + 1));
        }
        else 
        {
  
            // If N is even check
            // if N/2 i.e a 
            // is even or odd
            if (parseInt(a, 10) % 2 == 0) 
            {
  
                // If N/2 is even apply 
                // the previous formula
                document.write((Math.ceil(a) - 1) + 
                            " " + (Math.floor(a) + 1));
            
  
            else 
            {
                // If N/2 is odd answer 
                // will be ceil(N/2)-2 
                // and floor(N/2)+2
                document.write((Math.ceil(a) - 2) + 
                        " " + (Math.floor(a) + 2));
            }
        }
    }
      
    let n = 34;
    solve(n);
      
</script>


Output:  

15 19

Time Complexity: O(1), the code will run in O(1) time.
Auxiliary Space: O(1), no extra space is required, so it is a constant.

If you like neveropen and would like to contribute, you can also write an article using write.neveropen.co.uk or mail your article to review-team@neveropen.co.uk. See your article appearing on the neveropen 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.
 

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

Recent Comments