Count sub | Java implementation of the approach ; Function to count sub - arrays whose product is divisible by K ; Calculate the product of the current sub - array ; If product of the current sub - array is divisible by K ; Driver code
import java . util . * ; class GFG { static int countSubarrays ( int [ ] arr , int n , int K ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { long product = 1 ; for ( int x = i ; x <= j ; x ++ ) product *= arr [ x ] ; if ( product % K == 0 ) count ++ ; } } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 6 , 2 , 8 } ; int n = arr . length ; int K = 4 ; System . out . println ( countSubarrays ( arr , n , K ) ) ; } }