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++
#include <bits/stdc++.h>
using namespace std;
void findXandYwithminABSX_Y( int N)
{
if (N % 2 == 1) {
cout << (N / 2) << " " << (N / 2 + 1);
}
else {
cout << (N / 2 - 1) << " " << (N / 2 + 1);
}
}
int main()
{
int N = 12;
findXandYwithminABSX_Y(N);
}
|
Java
import java.util.*;
class GFG {
static void findXandYwithminABSX_Y( int N)
{
if (N % 2 == 1 ) {
System.out.print((N / 2 ) + " " + (N / 2 + 1 ));
}
else {
System.out.print((N / 2 - 1 ) + " "
+ (N / 2 + 1 ));
}
}
public static void main(String[] args)
{
int N = 12 ;
findXandYwithminABSX_Y(N);
}
}
|
Python3
def findXandYwithminABSX_Y(N):
if (N % 2 = = 1 ):
print ((N / / 2 ), (N / / 2 + 1 ))
else :
print ((N / / 2 - 1 ), (N / / 2 + 1 ))
if __name__ = = '__main__' :
N = 12
findXandYwithminABSX_Y(N)
|
C#
using System;
class GFG {
static void findXandYwithminABSX_Y( int N)
{
if (N % 2 == 1) {
Console.Write((N / 2) + " " + (N / 2 + 1));
}
else {
Console.Write((N / 2 - 1) + " " + (N / 2 + 1));
}
}
public static void Main()
{
int N = 12;
findXandYwithminABSX_Y(N);
}
}
|
PHP
<?php
function findXandYwithminABSX_Y( $N ){
if ( $N % 2 == 1)
{
return ( $N / 2) . " " . ( $N / 2 + 1);
}
else
{
return ( $N /2 -1) . " " . ( $N / 2 + 1);
}
}
$N = 12;
echo (findXandYwithminABSX_Y( $N ));
?>
|
Javascript
<script>
function findXandYwithminABSX_Y(N)
{
if (N % 2 == 1)
{
document.write((N / 2) + " " + (N / 2 + 1));
}
else
{
document.write((N / 2 - 1) + " " + (N / 2 + 1));
}
}
let N = 12;
findXandYwithminABSX_Y(N);
</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
print (min_abs_diff_pair_brute_force( 11 ))
print (min_abs_diff_pair_brute_force( 12 ))
|
time complexity of O(N^2)
space complexity of O(1),
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 geeks!