Introduction
Problem statement
We are given an array having N distinct positive integers and a range [L, R]. Our task is to find all the elements in the range [L, R] that are coprime with all given array elements in the given array.
Let us revisit what are coprime numbers:
Coprime Numbers
- A pair of numbers are coprime if they do not have any common factor other than 1.
- There should be at least 2 numbers to form a set of coprime numbers. For example, (5, 6), (7, 9), (3, 7) etc.
Sample test cases
Example 1:
Input: a[] = { 5, 6, 8, 9, 11, 2, 18 }, L = 5, R = 10
Output: 7
Explanation
{7} is the only element in the range of L to R, which is coprime with all given array elements
Example 2:
Input: a[] = { 7, 9, 4, 11, 22, 13, 16, 8, 24 }, L = 4, R = 18
Output: 17 5
Explanation
{17,5} are the elements in the range of L to R, which are coprime with all given array elements
Approach
The idea is simple, calculate all the divisors for every array element and store it in a container. Traverse the container and for every divisor, remove all the multiples of it from the range [L, R]. Finally, print all the numbers after removing every divisor's multiples from the range [L, R].
We will use an unordered set to store all the divisors for every array element.
Steps of algorithm
- Store all the divisors for every array element in an unordered set and remove 1 from it, say DIV.
- Store all the numbers in the range [L, R] in another unordered set, say NUMS.
- Traverse the unordered set DIV and for each element, remove all the multiples of that element from the NUMS if it is present in NUMS.
-
Print all the numbers present in NUMS, which are coprime with all given array elements.
Let’s understand the above approach with an example:
Given array = { 7, 9, 4, 11, 22, 13, 16, 8, 24 }, L = 4, R = 18
Steps:
- Store all the divisors for every element in DIV and remove 1 from DIV.
Divisors of 7 = {1, 7}
Divisors of 9 = {1, 3, 9}
Divisors of 4 = {1, 2, 4}
Divisors of 11 = {1, 11}
Divisors of 22 = {1, 2, 11, 22}
Divisors of 13 = {1, 13}
Divisors of 16 = {1, 2, 4, 8, 16}
Divisors of 8 = {1, 2, 4, 8}
Divisors of 24 = {1, 2, 3, 4, 6, 8, 12, 24}
DIV = { 6, 12, 8, 3, 9, 7, 24, 4, 2, 13, 11, 22, 16 }
- Store all the numbers from L to R in NUMS.
NUMS = { 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 }
-
Remove all the multiples of every element in DIV from NUMS.
Element in DIV (N) | All multiples of N <= R |
6 | 6, 12, 18 |
12 | 12 |
8 | 8, 16 |
3 | 3, 6, 9, 12, 15, 18 |
9 | 9, 18 |
7 | 7, 14 |
24 | |
4 | 4, 8, 12, 16 |
2 | 2, 4, 6, 8, 10, 12, 14, 16, 18 |
13 | 13 |
11 | 11 |
22 | |
16 | 16 |
After removing the multiples, NUMS = { 5, 17 }
-
Print all the numbers present in NUMS, which are coprime with all given array elements.