Find numbers in range [ L , R ] that are coprime with given Array elements | Java program for the above approach ; Function to find all the elements in the range [ L , R ] which are co prime with all array elements ; Store all the divisors of array element in S ; Find the divisors ; Stores all possible required number satisfying the given criteria ; Insert all element [ L , R ] ; Traverse the set ; Remove the multiples of ele ; Print the resultant numbers ; Driver Code
import java . util . * ; class GFG { static void elementsCoprimeWithArr ( int A [ ] , int N , int L , int R ) { HashSet < Integer > S = new HashSet < Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { int curr_ele = A [ i ] ; for ( int j = 1 ; j <= Math . sqrt ( curr_ele ) + 1 ; j ++ ) { if ( curr_ele % j == 0 ) { S . add ( j ) ; S . add ( curr_ele / j ) ; } } } HashSet < Integer > store = new HashSet < Integer > ( ) ; for ( int i = L ; i <= R ; i ++ ) store . add ( i ) ; S . remove ( 1 ) ; for ( int it : S ) { int ele = it ; int index = 1 ; while ( index * ele <= R ) { store . remove ( index * ele ) ; index ++ ; } } for ( int i : store ) { System . out . print ( i + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 5 } ; int L = 1 , R = 10 ; int N = arr . length ; elementsCoprimeWithArr ( arr , N , L , R ) ; } }