Find 5 non-intersecting ranges with given constraints
Last Updated :
17 Feb, 2023
Given Five integers A, B, C, D and E. the task is to find five non-intersecting ranges [X1, Y1], [X2, Y2], [X3, Y3], [X4, Y4], [X5, Y5] such that, the following 5 conditions hold.
- X1 + Y1 = min(A, B)
- X2 * Y2 = max(A, B)
- |X3 – Y3| = C
- ?X4 / Y4? = D
- X5 % Y5 = E
Examples:
Input: A = 6, B = 36, C = 20, D = 12, E = 55
Output: YES
Explanation: Let’s take, X1 = 3, Y1 = 3, X1+Y1 = 3+3 = 6.
And, X2 = 6, Y2 = 6, X2 *Y2 = 6*6 = 36.
And, X3 = 10, Y3 = 30, |X3 -Y3| = |10-30| = 20.
And, X4 = 480, Y4 = 40, ?X4 / Y4? = ?480/40? = 12.
And, X5 = 555, Y5 = 500, X5 % Y5 = 555 % 500 = 55.
So, all the 5 ranges [3, 3], [6, 6], [10, 30], [40, 480] and [500, 555] are non-intersecting.
Input: A = 6, B = 6, C = 6, D = 6, E = 6
Output: NO
Approach: The problem can be solved based on the following mathematical observation:
It is clear that 3rd, 4th and 5th condition always holds for any value of C, D and E as there are infinite non-intersecting ranges, so, we have to only check for sum and product.
The idea is that we have to reduce the size of both of the ranges as much as possible to minimize the chance of intersection. Because if the range with the minimum size is overlapping then it is sure that the other possible ranges should also overlap.
Follow the below steps to solve the problem:
- Set, prod = max(X, Y) and Sum = min(X, Y)
- If the Sum is even, then,
- Set L = sum/2 and R = sum/2
- Otherwise set L = sum/2 and R = (sum/2 +1).
- Now, run a loop from sqrt(prod) to 0:
- If, prod is divisible by i.
- Then, check if the range is overlapping then print No.
- Else print Yes.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void solve( int X, int Y, int Z, int D, int E)
{
int prod = max(X, Y);
int sum = min(X, Y);
int L, R;
if ((sum % 2) == 0) {
L = sum / 2;
R = sum / 2;
}
else {
L = sum / 2;
R = sum / 2 + 1;
}
for ( int i = sqrt (prod); i >= 0; i--)
if (prod % i == 0) {
if (i > R || L > (prod / i)) {
cout << "YES" << endl;
}
else {
cout << "NO" << endl;
}
return ;
}
}
int main()
{
int A = 6;
int B = 36;
int C = 20;
int D = 12;
int E = 55;
solve(A, B, C, D, E);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static void solve( int X, int Y, int Z, int D, int E)
{
int prod = Math.max(X, Y);
int sum = Math.min(X, Y);
int L, R;
if ((sum % 2 ) == 0 ) {
L = sum / 2 ;
R = sum / 2 ;
}
else {
L = sum / 2 ;
R = sum / 2 + 1 ;
}
for ( int i = ( int )Math.sqrt(prod); i >= 0 ; i--)
if (prod % i == 0 ) {
if (i > R || L > (prod / i)) {
System.out.println( "YES" );
}
else {
System.out.println( "NO" );
}
return ;
}
}
public static void main(String[] args)
{
int A = 6 ;
int B = 36 ;
int C = 20 ;
int D = 12 ;
int E = 55 ;
solve(A, B, C, D, E);
}
}
|
Python3
import math
def solve(X, Y, Z, D, E):
prod = max (X, Y);
sum = min (X, Y);
L = 0 ;
R = 0 ;
if (( sum % 2 ) = = 0 ) :
L = sum / 2 ;
R = sum / 2 ;
else :
L = sum / 2 ;
R = sum / 2 + 1 ;
j = ( int )(math.sqrt(prod));
for i in range (j, 0 , - 1 ):
if (prod % i = = 0 ):
if (i > R or L > (prod / i)) :
print ( "YES" ) ;
else :
print ( "NO" ) ;
return ;
A = 6 ;
B = 36 ;
C = 20 ;
D = 12 ;
E = 55 ;
solve(A, B, C, D, E);
|
C#
using System;
public class GFG {
static void solve( int X, int Y, int Z, int D, int E)
{
int prod = Math.Max(X, Y);
int sum = Math.Min(X, Y);
int L, R;
if ((sum % 2) == 0) {
L = sum / 2;
R = sum / 2;
}
else {
L = sum / 2;
R = sum / 2 + 1;
}
for ( int i = ( int )Math.Sqrt(prod); i >= 0; i--)
if (prod % i == 0) {
if (i > R || L > (prod / i)) {
Console.WriteLine( "YES" );
}
else {
Console.WriteLine( "NO" );
}
return ;
}
}
static public void Main()
{
int A = 6;
int B = 36;
int C = 20;
int D = 12;
int E = 55;
solve(A, B, C, D, E);
}
}
|
Javascript
function solve(X, Y, Z, D, E) {
let prod = Math.max(X, Y);
let sum = Math.min(X, Y);
let L, R;
if (sum % 2 === 0) {
L = sum / 2;
R = sum / 2;
} else {
L = Math.floor(sum / 2);
R = Math.floor(sum / 2) + 1;
}
for (let i = Math.floor(Math.sqrt(prod)); i >= 0; i--) {
if (prod % i === 0) {
if (i > R || L > prod / i) {
console.log( "YES" );
} else {
console.log( "NO" );
}
return ;
}
}
}
let A = 6;
let B = 36;
let C = 20;
let D = 12;
let E = 55;
solve(A, B, C, D, E);
|
Time Complexity: O(sqrt(max(A, B)))
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...