Generate an array with product of all subarrays of length exceeding one divisible by K | Java program for the above approach ; Function to check if the required array can be generated or not ; To check if divisor exists ; To store divisiors of K ; Check if K is prime or not ; If array can be generated ; Print d1 and d2 alternatively ; No such array can be generated ; Driver Code ; Given N and K ; Function Call
class GFG { public static void array_divisbleby_k ( int N , int K ) { boolean flag = false ; int d1 = 0 , d2 = 0 ; for ( int i = 2 ; i * i <= K ; i ++ ) { if ( K % i == 0 ) { flag = true ; d1 = i ; d2 = K / i ; break ; } } if ( flag ) { for ( int i = 0 ; i < N ; i ++ ) { if ( i % 2 == 1 ) { System . out . print ( d2 + " ▁ " ) ; } else { System . out . print ( d1 + " ▁ " ) ; } } } else { System . out . print ( - 1 ) ; } } public static void main ( String [ ] args ) { int N = 5 , K = 21 ; array_divisbleby_k ( N , K ) ; } }