Open in App
Not now

# 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++

 `// C++ code for above approach`   `#include ` `using` `namespace` `std;`   `// Function to check whether there is any` `// non-intersecting ranges exist or not` `void` `solve(``int` `X, ``int` `Y, ``int` `Z, ``int` `D, ``int` `E)` `{`   `    ``// find Product` `    ``int` `prod = max(X, Y);`   `    ``// find Sum` `    ``int` `sum = min(X, Y);` `    ``int` `L, R;`   `    ``// Select smallest size range` `    ``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``;` `        ``}` `}`   `// Driver Code`   `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

 `// Java code to implement the approach`   `import` `java.io.*;` `import` `java.util.*;`   `class` `GFG {`   `// Function to check whether there is any` `// non-intersecting ranges exist or not` `static` `void` `solve(``int` `X, ``int` `Y, ``int` `Z, ``int` `D, ``int` `E)` `{`   `    ``// find Product` `    ``int` `prod = Math.max(X, Y);`   `    ``// find Sum` `    ``int` `sum = Math.min(X, Y);` `    ``int` `L, R;`   `    ``// Select smallest size range` `    ``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``;` `        ``}` `}`   `    `  `// Driver code` `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

 `# Python code for above approach`   `import` `math` `# Function to check whether there is any` `# non-intersecting ranges exist or not` `def` `solve(X, Y, Z, D, E):` `    ``# find Product` `    ``prod ``=` `max``(X, Y);`   `    ``# find Sum` `    ``sum` `=` `min``(X, Y);` `    ``L``=``0``;` `    ``R``=``0``;`   `    ``# Select smallest size range` `    ``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``;` `            `  `# Driver Code`   `A ``=` `6``;` `B ``=` `36``;` `C ``=` `20``;` `D ``=` `12``;` `E ``=` `55``;`   `solve(A, B, C, D, E);`

## C#

 `// C# code to implement the approach` `using` `System;` `public` `class` `GFG {`   `  ``// Function to check whether there is any` `  ``// non-intersecting ranges exist or not` `  ``static` `void` `solve(``int` `X, ``int` `Y, ``int` `Z, ``int` `D, ``int` `E)` `  ``{`   `    ``// find Product` `    ``int` `prod = Math.Max(X, Y);`   `    ``// find Sum` `    ``int` `sum = Math.Min(X, Y);` `    ``int` `L, R;`   `    ``// Select smallest size range` `    ``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()` `  ``{`   `    ``// Code` `    ``int` `A = 6;` `    ``int` `B = 36;` `    ``int` `C = 20;` `    ``int` `D = 12;` `    ``int` `E = 55;`   `    ``solve(A, B, C, D, E);` `  ``}` `}`   `// This code is contributed by lokeshmvs21.`

## Javascript

 `// JavaScript equivalent to the given C++ code`   `// Function to check whether there is any non-intersecting ranges exist or not` `function` `solve(X, Y, Z, D, E) {` `  ``// find Product` `  ``let prod = Math.max(X, Y);`   `  ``// find Sum` `  ``let sum = Math.min(X, Y);` `  ``let L, R;`   `  ``// Select smallest size range` `  ``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``;` `    ``}` `  ``}` `}`   `// Driver Code`   `let A = 6;` `let B = 36;` `let C = 20;` `let D = 12;` `let E = 55;`   `solve(A, B, C, D, E);`   `//code by ksam24000`

Output

`YES`

Time Complexity: O(sqrt(max(A, B)))
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up
Related Articles