Find a pair with sum N having minimum absolute difference
Last Updated :
20 Apr, 2023
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 <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);
}
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
<?php
function findXandYwithminABSX_Y($N){
// If N is an odd number
if ($N % 2 == 1)
{
return ($N / 2) . " " . ($N / 2 + 1);
}
// If N is an even number
else
{
return ($N /2 -1) . " " . ($N / 2 + 1);
}
}
// Driver code
$N = 12;
echo(findXandYwithminABSX_Y($N));
?>
JavaScript
<script>
// JavaScript program to implement the above approach
// Function to find the value of X and Y
// having minimum value of abs(X - Y)
function findXandYwithminABSX_Y(N)
{
// If N is an odd number
if (N % 2 == 1)
{
document.write((N / 2) + " " + (N / 2 + 1));
}
// If N is an even number
else
{
document.write((N / 2 - 1) + " " + (N / 2 + 1));
}
}
// Driver Code
let N = 12;
findXandYwithminABSX_Y(N);
// This code is contributed by susmitakundugoaldanga.
</script>
Time Complexity: O(1)
Auxiliary Space: O(1)
using Brute Force in python:
Approach:
- Initialize a variable min_diff to infinity.
- Use two nested loops to generate all possible pairs of numbers (i, j) such that 1 <= i < j <= N.
- For each pair (i, j), check if i + j == N.
- If i + j == N, calculate the absolute difference between i and j using abs(i - j).
- If the absolute difference is less than the current minimum difference min_diff, update min_diff to the new absolute difference and store the pair (i, j) as the new answer.
- After checking all possible pairs, return the answer as a tuple (x, y) where x and y are the pair of numbers that add up to N with the minimum absolute difference.
Python3
def min_abs_diff_pair_brute_force(N):
min_diff = float('inf')
for i in range(1, N):
for j in range(i+1, N+1):
if abs(i-j) < min_diff and i+j == N:
min_diff = abs(i-j)
x, y = i, j
return x, y
# Test the function with the given inputs
print(min_abs_diff_pair_brute_force(11)) # Output: (5, 6)
print(min_abs_diff_pair_brute_force(12)) # Output: (5, 7)
time complexity of O(N^2)
space complexity of O(1),
Similar Reads
Pair of fibonacci numbers with a given sum and minimum absolute difference Given an integer N(less than 10^6), the task is to find a pair of Fibonacci numbers whose sum is equal to the given N, and the absolute difference between the chosen pair is minimum. Print -1 if there is no solution. Examples: Input: N = 199 Output: 55 144 Explanation 199 can be represented as sum 5
8 min read
Pair of prime numbers with a given sum and minimum absolute difference Given an integer 'sum' (less than 10^8), the task is to find a pair of prime numbers whose sum is equal to the given 'sum' Out of all the possible pairs, the absolute difference between the chosen pair must be minimum. If the âsumâ cannot be represented as a sum of two prime numbers then print âCann
8 min read
Find maximum absolute difference with min sum of ratios in an Array Given an array of positive integers arr[], the task is to find the maximum absolute difference between the pairs from the array such that the sum of their ratios is minimum. Examples: Input: arr[] = {2, 6, 3, 4, 8, 9}Output: 4Explanation: In the above example, the ratios of every pair are calculated
7 min read
Find all pairs in an Array in sorted order with minimum absolute difference Given an integer array arr[] of size N, the task is to find all distinct pairs having minimum absolute difference and print them in ascending order. Examples:Input: arr[] = {4, 2, 1, 3}Output: {1, 2}, {2, 3}, {3, 4}Explanation: The minimum absolute difference between pairs is 1.Input: arr[] = {1, 3,
9 min read
Co-prime pair with given sum minimum difference Co-prime or mutually prime pairs are those pairs of numbers whose GCD is 1. Given a number n represent the number as the sum of a Co-prime pair (A, B) such that A - B is minimum. Examples : Input : 12 Output : 5 7 Possible co-prime pairs are (5, 7), (1, 11) but difference between 5 and 7 is minimum
5 min read
Minimum and Maximum sum of absolute differences of pairs Given an array of N integers where N is even, find the minimum and maximum sum of absolute difference of N/2 pairs formed by pairing every element with one other element. Examples: Input: a[] = {10, -10, 20, -40} Output: min_sum = 40, max_sum = 80 Explanation: Pairs selected for minimum sum (-10, -4
8 min read