Given three positive integers A, B and N, the task is to split N into two parts such that they become equal i.e. find two positive integers X and Y, such that X + Y = N and A + X = B + Y. If no such pair exists, print -1.
Input: A = 1, B = 3, N = 4
Output: 3 1
Explanation: If X = 3 and Y = 1, then A + X = B + Y and X + Y =4
Input: A = 1, B = 3, N = 1
Output: -1
- Define a function find_pairs that takes in three parameters: A, B, and N.
- Initialize two nested loops that iterate over the integers from 1 to N-1.
- Check if the sum of the two loop variables equals N and the sum of A and the first loop variable
- equals the sum of B and the second loop variable.
- If the conditions are satisfied, return the two loop variables as a tuple.
- If no such pair is found, return -1.
- Assign the output of find_pairs with the values of A, B, and N to the variable result.
- Print result.
C++
#include <iostream>
std::pair< int , int > findPairs( int A, int B, int N) {
for ( int i = 1; i < N; i++) {
for ( int j = 1; j < N; j++) {
if (i + j == N && A + i == B + j) {
return std::make_pair(i, j);
}
}
}
return std::make_pair(-1, -1);
}
int main() {
int A = 1;
int B = 3;
int N = 1;
std::pair< int , int > result = findPairs(A, B, N);
if (result.first != -1) {
std::cout << "Pair found: (" << result.first << ", " << result.second << ")" << std::endl;
} else {
std::cout << -1 << std::endl;
}
return 0;
}
|
Java
public class Main {
public static int [] findPairs( int A, int B, int N) {
for ( int i = 1 ; i < N; i++) {
for ( int j = 1 ; j < N; j++) {
if (i + j == N && A + i == B + j) {
return new int []{i, j};
}
}
}
return new int []{- 1 , - 1 };
}
public static void main(String[] args) {
int A = 1 ;
int B = 3 ;
int N = 1 ;
int [] result = findPairs(A, B, N);
if (result[ 0 ] != - 1 ) {
System.out.println( "Pair found: (" + result[ 0 ] + ", " + result[ 1 ] + ")" );
} else {
System.out.println(- 1 );
}
}
}
|
Python3
def find_pairs(A, B, N):
for i in range ( 1 , N):
for j in range ( 1 , N):
if i + j = = N and A + i = = B + j:
return i, j
return - 1
A = 1
B = 3
N = 1
result = find_pairs(A, B, N)
print (result)
|
C#
using System;
class Program
{
static Tuple< int , int > FindPairs( int A, int B, int N)
{
for ( int i = 1; i < N; i++)
{
for ( int j = 1; j < N; j++)
{
if (i + j == N && A + i == B + j)
{
return Tuple.Create(i, j);
}
}
}
return Tuple.Create(-1, -1);
}
static void Main()
{
int A = 1;
int B = 3;
int N = 1;
Tuple< int , int > result = FindPairs(A, B, N);
if (result.Item1 != -1)
{
Console.WriteLine($ "Pair found: ({result.Item1}, {result.Item2})" );
}
else
{
Console.WriteLine(-1);
}
}
}
|
Javascript
function findPairs(A, B, N) {
for (let i = 1; i < N; i++) {
for (let j = 1; j < N; j++) {
if (i + j === N && A + i === B + j) {
return [i, j];
}
}
}
return [-1];
}
const A = 1;
const B = 3;
const N = 1;
const result = findPairs(A, B, N);
console.log(result);
|
Time complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach:
It can be observed that, since X + Y = N and A + X = B + Y, then X can be expressed as (N + B – A) / 2. Simply check if (N + B – A) / 2 is even or not. If it is even, calculate X and corresponding Y. Otherwise, print -1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findPair( int A, int B, int N)
{
int X, Y;
X = N - B + A;
if (X % 2 != 0) {
cout << "-1" ;
}
else {
X = X / 2;
Y = N - X;
cout << X << " " << Y;
}
}
int main()
{
int A = 1;
int B = 3;
int N = 4;
findPair(A, B, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void findPair( int A, int B, int N)
{
int X, Y;
X = N - B + A;
if (X % 2 != 0 )
{
System.out.print( "-1" );
}
else
{
X = X / 2 ;
Y = N - X;
System.out.print(X + " " + Y);
}
}
public static void main (String[] args)
{
int A = 1 ;
int B = 3 ;
int N = 4 ;
findPair(A, B, N);
}
}
|
Python3
def findPair(A, B, N):
X = N - B + A
if (X % 2 ! = 0 ):
print ( "-1" )
else :
X = X / / 2
Y = N - X
print (X , Y)
if __name__ = = "__main__" :
A = 1
B = 3
N = 4
findPair(A, B, N)
|
C#
using System;
class GFG{
static void findPair( int A, int B, int N)
{
int X, Y;
X = N - B + A;
if (X % 2 != 0)
{
Console.Write( "-1" );
}
else
{
X = X / 2;
Y = N - X;
Console.Write(X + " " + Y);
}
}
public static void Main( string [] args)
{
int A = 1;
int B = 3;
int N = 4;
findPair(A, B, N);
}
}
|
Javascript
<script>
function findPair(A , B , N) {
var X, Y;
X = N - B + A;
if (X % 2 != 0) {
document.write( "-1" );
}
else {
X = X / 2;
Y = N - X;
document.write(X + " " + Y);
}
}
var A = 1;
var B = 3;
var N = 4;
findPair(A, B, N);
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Contact Us