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