Related Articles

# Find a pair with sum N having minimum absolute difference

• Last Updated : 19 Mar, 2021

Given an integer N, the task is to find a distinct pair of X and Y such that X + Y = N and abs(X – Y) is minimum.

Examples:

Input: N = 11
Output: 5 6
Explanation:
X = 5 and Y = 6 satisfy the given equation.
Therefore, the minimum absolute value of abs(X – Y) = 1.

Input: N = 12
Output: 5 7

Naive Approach: The simplest approach to solve this problem is to generate all possible values of X and Y with a sum equal to N and print the value of X and Y which gives the minimum absolute value of abs(X – Y).

Time Complexity: O(N2
Auxiliary Space: O(1)

Efficient Approach: The above approach can be optimized based on the following observations:

If N % 2 == 1, then pairs (N / 2) and (N / 2 + 1) have minimum absolute difference.
Otherwise, pairs (N / 2 – 1) and (N / 2 + 1) will have the minimum absolute difference.

Follow the steps below to solve the problem:

• Check if N is odd or not. If found to be true, then print the floor value of (N / 2) and (N / 2 + 1) as the required answer.
• Otherwise, print the value of (N / 2 – 1) and (N / 2 + 1).

Below is the implementation of the above approach:

## C++

 `// C++ program to implement``// the above approach``#include ``using` `namespace` `std;` `// Function to find the value of X and Y``// having minimum value of abs(X - Y)``void` `findXandYwithminABSX_Y(``int` `N)``{``    ``// If N is an odd number``    ``if` `(N % 2 == 1) {``        ``cout << (N / 2) << ``" "` `<< (N / 2 + 1);``    ``}` `    ``// If N is an even number``    ``else` `{``        ``cout << (N / 2 - 1) << ``" "` `<< (N / 2 + 1);``    ``}``}` `// Driver Code``int` `main()``{``    ``int` `N = 12;``    ``findXandYwithminABSX_Y(N);``}`

## Java

 `// Java program to implement``// the above approach``import` `java.util.*;``class` `GFG {` `    ``// Function to find the value``    ``// of X and Y having minimum``    ``// value of Math.abs(X - Y)``    ``static` `void` `findXandYwithminABSX_Y(``int` `N)``    ``{``        ``// If N is an odd number``        ``if` `(N % ``2` `== ``1``) {``            ``System.out.print((N / ``2``) + ``" "` `+ (N / ``2` `+ ``1``));``        ``}` `        ``// If N is an even number``        ``else` `{``            ``System.out.print((N / ``2` `- ``1``) + ``" "``                             ``+ (N / ``2` `+ ``1``));``        ``}``    ``}` `    ``// Driver Code``    ``public` `static` `void` `main(String[] args)``    ``{``        ``int` `N = ``12``;``        ``findXandYwithminABSX_Y(N);``    ``}``}` `// This code is contributed by gauravrajput1`

## Python3

 `# Python3 program to implement``# the above approach` `# Function to find the value of X and Y``# having minimum value of abs(X - Y)`  `def` `findXandYwithminABSX_Y(N):` `    ``# If N is an odd number``    ``if` `(N ``%` `2` `=``=` `1``):``        ``print``((N ``/``/` `2``), (N ``/``/` `2` `+` `1``))` `    ``# If N is an even number``    ``else``:``        ``print``((N ``/``/` `2` `-` `1``), (N ``/``/` `2` `+` `1``))`  `# Driver Code``if` `__name__ ``=``=` `'__main__'``:` `    ``N ``=` `12` `    ``findXandYwithminABSX_Y(N)` `# This code is contributed by mohit kumar 29`

## C#

 `// C# program to implement``// the above approach``using` `System;``class` `GFG {` `    ``// Function to find the value``    ``// of X and Y having minimum``    ``// value of Math.abs(X - Y)``    ``static` `void` `findXandYwithminABSX_Y(``int` `N)``    ``{``        ``// If N is an odd number``        ``if` `(N % 2 == 1) {``            ``Console.Write((N / 2) + ``" "` `+ (N / 2 + 1));``        ``}` `        ``// If N is an even number``        ``else` `{``            ``Console.Write((N / 2 - 1) + ``" "` `+ (N / 2 + 1));``        ``}``    ``}` `    ``// Driver Code``    ``public` `static` `void` `Main()``    ``{``        ``int` `N = 12;``        ``findXandYwithminABSX_Y(N);``    ``}``}` `// This code is contributed by bgangwar59`

## PHP

 ``

## Javascript

 ``
Output:
`5 7`

Time Complexity: O(1)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Join the First-Step-to-DSA Course for Class 9 to 12 students , specifically designed to introduce data structures and algorithms to the class 9 to 12 students

My Personal Notes arrow_drop_up