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 <bits/stdc++.h> 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
Time Complexity: O(sqrt(max(A, B)))
Auxiliary Space: O(1)
Similar Reads
Find a pair of intersecting ranges from a given array
Given a 2D array ranges[][] of size N * 2, with each row representing a range of the form [L, R], the task is to find two ranges such that the first range completely lies ins the second range and print their indices. If no such pair of ranges can be obtained, print -1. If multiple such ranges exist,
10 min read
Find the count of distinct numbers in a range
Given an array of size N containing numbers only from 0 to 63, and you are asked Q queries regarding it.Queries are as follows: 1 X Y i.e Change the element at index X to Y2 L R i.e Print the count of distinct elements present in between L and R inclusive Examples: Input: N = 7 ar = {1, 2, 1, 3, 1,
15 min read
Forming smallest array with given constraints
Given three integers x, y and z (can be negative). The task is to find the length of the smallest array that can be made such that absolute difference between adjacent elements is less than or equal to 1, the first element of the array is x, having one integer y and last element z. Examples: Input :
4 min read
Find all the intersecting pairs from a given array
Given n pairs (S[i], F[i]) where for every i, S[i]< F[i]. Two ranges are said to intersect if and only if either of them does not fully lie inside the other one that is only one point of a pair lies between the start and end of the other pair. We have to print all the intersecting ranges for each
14 min read
Find the missing elements from 1 to M in given N ranges
Given N segments as ranges [L, R] where ranges are non-intersecting and non-overlapping. The task is to find all number between 1 to M that doesn't belong to any of the given ranges. Examples: Input : N = 2, M = 6 Ranges: [1, 2] [4, 5] Output : 3, 6 Explanation: Only 3 and 6 are missing from the abo
8 min read
Check if there exists two non-intersect ranges
Given two non-negative integers X and Y, the task is to output the two non-intersecting ranges of non-negative integers such that the sum of the starting and ending point of the first range is equal to X and the product of the starting and ending point of the second range is equal Y. If there is no
7 min read
Find the largest interval that contains exactly one of the given N integers.
Given an array arr[] of N distinct integers, the task is to find the maximum element in an interval [L, R] such that the interval contains exactly one of the given N integers and 1 ? L ? R ? 105 Input: arr[] = {5, 10, 200} Output: 99990 All possible intervals are [1, 9], [6, 199] and [11, 100000]. [
5 min read
Count numbers that does not contain digit N in given range
Given integers, N, L, and R, the task is to find the number of integers in the range L to R that does not contain the digit N. print the answer modulo 109 + 7. ( L ? R ? 101000000) Examples: Input: N = 5, L = 1, R = 10Output: 9Explanation: excluding all 5 others from 1 to 10 will be included in the
14 min read
Find a range of composite numbers of given length
Given an integer n, we need to find a range of positive integers such that all the number in that range are composite and length of that range is n. You may print anyone range in the case of more than one answer. A composite number is a positive integer that has at least one divisor other than 1 and
6 min read
Find smallest range containing at least 1 elements from given N ranges
Given N ranges of the form [L, R], the task is to find the range having the minimum number of integers such that at least one point of all the given N ranges exists in that range. Example: Input: ranges[] = {{1, 6}, {2, 7}, {3, 8}, {4, 9}}Output: 6 6Explanation: All the given ranges contain 6 as an
7 min read