Find X and Y from their sum, difference, product, division and remainder
Given arr[] of 5 integers denoting the values of X+Y, X−Y, X*Y, X%Y and ⌊X/Y⌋ in sorted order for two non-zero integers X and Y, the task is to find the value of X and Y.
Note: If no solution exists return two 0s
Examples:
Input: -1, 0, 4, 9, 20
Output: X = 4, Y = 5
Explanation: If we consider, X + Y = 9, X – Y = -1.
Then X and Y are 4 and 5, which satisfies the condition
X * Y = 20, X % Y = 4, Floor(X / Y) = 0.
Input: -3, -1, 0, 2, 2
Output: X = -2, Y = -1
Approach: The problem can be solved based on the following mathematical observation
If we have X+Y and X-Y we can find X and Y.
X = [(X+Y)+(X-Y)]/2
Y = X+Y-X
To implement the above idea try all possible pairs of values for X+Y and X-Y using backtracking. Follow the below steps to solve the problem:
- Consider the array elements as A, B, C, D, E
- Try all possible pairs for the value of X+Y and X-Y.
- Find X and Y from those two values based on the above observation.
- If those two values of X and Y satisfy the other values, then return.
- Otherwise, try for another pair.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
#define ll long long
using namespace std;
pair<ll, ll> isValid(ll A, ll B, ll C, ll D, ll E)
{
ll a = A + B;
if ( ceil ( float (a / 2)) != floor ( float (a / 2))) {
return make_pair(0, 0);
}
else {
a = a / 2;
ll b = A - a;
if (a == 0 || b == 0)
return make_pair(0, 0);
else if ((a + b) > pow (10, 3)
|| (a - b) < pow (-10, 3))
return make_pair(0, 0);
else if ((a * b == C) && (a / b == D)
&& (a % b == E)
|| (a * b == C) && (a / b == E)
&& (a % b == D))
return make_pair(a, b);
else if ((a * b == D) && (a / b == C)
&& (a % b == E)
|| (a * b == D) && (a / b == E)
&& (a % b == C))
return make_pair(a, b);
else if ((a * b == E) && (a / b == C)
&& (a % b == D)
|| (a * b == E) && (a / b == D)
&& (a % b == C))
return make_pair(a, b);
else
return make_pair(0, 0);
}
}
void findNum(ll* arr)
{
pair<ll, ll> p;
bool flag = 0;
for ( int i = 0; i <= 4; i++) {
swap(arr[0], arr[i]);
for ( int j = 1; j <= 4; j++) {
swap(arr[1], arr[j]);
p = isValid(arr[0], arr[1],
arr[2], arr[3],
arr[4]);
if ((p.first != 0)
&& (p.second != 0)) {
flag = 1;
cout << p.first << " "
<< p.second << endl;
}
swap(arr[1], arr[j]);
if (flag)
break ;
}
swap(arr[0], arr[i]);
if (flag)
break ;
}
if (!flag)
cout << 0 << " " << 0 << endl;
}
int main()
{
int N = 5;
ll arr[N] = { -1, 0, 4, 9, 20 };
findNum(arr);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
public static long [] isValid( long A, long B, long C,
long D, long E)
{
long a = A + B;
if (Math.ceil((a / 2.0 )) != Math.floor((a / 2.0 ))) {
long ans[] = { 0 , 0 };
return ans;
}
else {
a = a / 2 ;
long b = A - a;
long res[] = { 0 , 0 };
long res1[] = { a, b };
if (a == 0 || b == 0 )
return res;
else if ((a + b) > Math.pow( 10 , 3 )
|| (a - b) < Math.pow(- 10 , 3 ))
return res;
else if ((a * b == C) && (a / b == D)
&& (a % b == E)
|| (a * b == C) && (a / b == E)
&& (a % b == D))
return res1;
else if ((a * b == D) && (a / b == C)
&& (a % b == E)
|| (a * b == D) && (a / b == E)
&& (a % b == C))
return res1;
else if ((a * b == E) && (a / b == C)
&& (a % b == D)
|| (a * b == E) && (a / b == D)
&& (a % b == C))
return res1;
else
return res;
}
}
public static void findNum( long arr[])
{
long p[] = new long [ 2 ];
int flag = 0 ;
for ( int i = 0 ; i <= 4 ; i++) {
long tmp = arr[ 0 ];
arr[ 0 ] = arr[i];
arr[i] = tmp;
for ( int j = 1 ; j <= 4 ; j++) {
tmp = arr[ 1 ];
arr[ 1 ] = arr[j];
arr[j] = tmp;
p = isValid(arr[ 0 ], arr[ 1 ], arr[ 2 ], arr[ 3 ],
arr[ 4 ]);
if ((p[ 0 ] != 0 ) && (p[ 1 ] != 0 )) {
flag = 1 ;
System.out.println(p[ 0 ] + " " + p[ 1 ]);
}
tmp = arr[ 1 ];
arr[ 1 ] = arr[j];
arr[j] = tmp;
if (flag != 0 )
break ;
}
tmp = arr[ 0 ];
arr[ 0 ] = arr[i];
arr[i] = tmp;
if (flag != 0 )
break ;
}
if (flag == 0 )
System.out.println( "0 0" );
}
public static void main(String[] args)
{
int N = 5 ;
long arr[] = { - 1 , 0 , 4 , 9 , 20 };
findNum(arr);
}
}
|
Python3
import math
def isValid(A, B, C, D, E) :
a = A + B
if (math.ceil((a / 2.0 )) ! = math.floor((a / 2.0 ))) :
ans = [ 0 , 0 ]
return ans
else :
a = a / / 2
b = A - a
res = [ 0 , 0 ]
res1 = [ a, b ]
if (a = = 0 or b = = 0 ) :
return res
elif ((a + b) > pow ( 10 , 3 )
or (a - b) < pow ( - 10 , 3 )) :
return res
elif ((a * b = = C) and (a / / b = = D)
and (a % b = = E)
or (a * b = = C) and (a / / b = = E)
and (a % b = = D)) :
return res1
elif ((a * b = = D) and (a / / b = = C)
and (a % b = = E)
or (a * b = = D) and (a / / b = = E)
and (a % b = = C)) :
return res1
elif ((a * b = = E) and (a / / b = = C)
and (a % b = = D)
or (a * b = = E) and (a / / b = = D)
and (a % b = = C)) :
return res1
else :
return res
def findNum(arr) :
p = [ 0 ] * 2
flag = 0
for i in range ( 0 , 5 , 1 ) :
tmp = arr[ 0 ]
arr[ 0 ] = arr[i]
arr[i] = tmp
for j in range ( 1 , 5 , 1 ) :
tmp = arr[ 1 ]
arr[ 1 ] = arr[j]
arr[j] = tmp
p = isValid(arr[ 0 ], arr[ 1 ], arr[ 2 ], arr[ 3 ], arr[ 4 ])
if ((p[ 0 ] ! = 0 ) and (p[ 1 ] ! = 0 )) :
flag = 1
print (p[ 0 ], end = " " )
print (p[ 1 ])
tmp = arr[ 1 ]
arr[ 1 ] = arr[j]
arr[j] = tmp
if (flag ! = 0 ) :
break
tmp = arr[ 0 ]
arr[ 0 ] = arr[j]
arr[i] = tmp
if (flag ! = 0 ) :
break
if (flag = = 0 ) :
print ( "0 0" )
if __name__ = = "__main__" :
N = 5
arr = [ - 1 , 0 , 4 , 9 , 20 ]
findNum(arr)
|
C#
using System;
public class GFG{
static long [] isValid( long A, long B, long C,
long D, long E)
{
long a = A + B;
if (Math.Ceiling((a / 2.0)) != Math.Floor((a / 2.0))) {
long [] ans = { 0, 0 };
return ans;
}
else {
a = a / 2;
long b = A - a;
long [] res = { 0, 0 };
long [] res1 = { a, b };
if (a == 0 || b == 0)
return res;
else if ((a + b) > Math.Pow(10, 3)
|| (a - b) < Math.Pow(-10, 3))
return res;
else if ((a * b == C) && (a / b == D)
&& (a % b == E)
|| (a * b == C) && (a / b == E)
&& (a % b == D))
return res1;
else if ((a * b == D) && (a / b == C)
&& (a % b == E)
|| (a * b == D) && (a / b == E)
&& (a % b == C))
return res1;
else if ((a * b == E) && (a / b == C)
&& (a % b == D)
|| (a * b == E) && (a / b == D)
&& (a % b == C))
return res1;
else
return res;
}
}
static void findNum( long [] arr)
{
long [] p = new long [2];
int flag = 0;
for ( int i = 0; i <= 4; i++) {
long tmp = arr[0];
arr[0] = arr[i];
arr[i] = tmp;
for ( int j = 1; j <= 4; j++) {
tmp = arr[1];
arr[1] = arr[j];
arr[j] = tmp;
p = isValid(arr[0], arr[1], arr[2], arr[3],
arr[4]);
if ((p[0] != 0) && (p[1] != 0)) {
flag = 1;
Console.WriteLine(p[0] + " " + p[1]);
}
tmp = arr[1];
arr[1] = arr[j];
arr[j] = tmp;
if (flag != 0)
break ;
}
tmp = arr[0];
arr[0] = arr[i];
arr[i] = tmp;
if (flag != 0)
break ;
}
if (flag == 0)
Console.WriteLine( "0 0" );
}
static public void Main (){
long [] arr = { -1, 0, 4, 9, 20 };
findNum(arr);
}
}
|
Javascript
<script>
function isValid(A, B, C, D, E)
{
let a = A + B;
if (Math.ceil((a / 2)) != Math.floor((a / 2))) {
let ans = [ 0, 0 ];
return ans;
}
else {
a = Math.floor(a / 2);
let b = A - a;
let res = [ 0, 0 ];
let res1 = [ a, b ];
if (a == 0 || b == 0)
return res;
else if ((a + b) > Math.pow(10, 3)
|| (a - b) < Math.pow(-10, 3))
return res;
else if ((a * b == C) && (Math.floor(a / b) == D)
&& (a % b == E)
|| (a * b == C) && (Math.floor(a / b) == E)
&& (a % b == D))
return res1;
else if ((a * b == D) && (Math.floor(a / b) == C)
&& (a % b == E)
|| (a * b == D) && (Math.floor(a / b) == E)
&& (a % b == C))
return res1;
else if ((a * b == E) && (Math.floor(a / b) == C)
&& (a % b == D)
|| (a * b == E) && (Math.floor(a / b) == D)
&& (a % b == C))
return res1;
else
return res;
}
}
function findNum(arr)
{
let p = new Array(2);
let flag = 0;
for (let i = 0; i <= 4; i++) {
let tmp = arr[0];
arr[0] = arr[i];
arr[i] = tmp;
for (let j = 1; j <= 4; j++) {
tmp = arr[1];
arr[1] = arr[j];
arr[j] = tmp;
p = isValid(arr[0], arr[1], arr[2], arr[3],
arr[4]);
if ((p[0] != 0) && (p[1] != 0)) {
flag = 1;
document.write(p[0] + " " + p[1]);
}
tmp = arr[1];
arr[1] = arr[j];
arr[j] = tmp;
if (flag != 0)
break ;
}
tmp = arr[0];
arr[0] = arr[i];
arr[i] = tmp;
if (flag != 0)
break ;
}
if (flag == 0)
document.write( "0 0" );
}
let N = 5;
let arr = [ -1, 0, 4, 9, 20 ];
findNum(arr);
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
16 May, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...