Given an integer N, the task is 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 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 an 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<bits/stdc++.h> 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); } |
5 7
Time Complexity: O(1)
Auxiliary Space: O(1)
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.
Recommended Posts:
- Count of subarrays of size K having at least one pair with absolute difference divisible by K-1
- Pair of prime numbers with a given sum and minimum absolute difference
- Pair of integers having least GCD among all given pairs having GCD exceeding K
- Pair with min absolute difference and whose product is N+1 or N+2
- Product of absolute difference of every pair in given Array
- Split array into minimum number of subsets having maximum pair sum at most K
- Count of numbers upto N having absolute difference of at most K between any two adjacent digits
- Generate all N digit numbers having absolute difference as K between adjacent digits
- Length of longest subsequence having absolute difference of all pairs divisible by K
- Count all disjoint pairs having absolute difference at least K from a given array
- Minimum sum of absolute difference of pairs of two arrays
- Sequence with sum K and minimum sum of absolute differences between consecutive elements
- Pair formation such that maximum pair sum is minimized
- Number of indices pair such that element pair sum from first Array is greater than second Array
- Pair having all other given pairs lying between its minimum and maximum
- Maximise sum of absolute difference between adjacent elements in Array with sum K
- Pair of integers having difference of their fifth power as X
- Pair of integers with difference K having an element as the K-th multiple of the other
- Co-prime pair with given sum minimum difference
- Divide Matrix into K groups of adjacent cells having minimum difference between maximum and minimum sized groups
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

