Count of triplets ( a , b , c ) in the Array such that a divides b and b divides c | Java program to find count of triplets ( a , b , c ) in the Array such that a divides b and b divides c ; Function to count triplets ; Iterate for middle element ; Iterate left array for a [ i ] ; Iterate right array for a [ k ] ; return the final result ; Driver code
import java . io . * ; import java . util . * ; class GFG { static int getCount ( int arr [ ] , int n ) { int count = 0 ; for ( int j = 1 ; j < n - 1 ; j ++ ) { int p = 0 , q = 0 ; for ( int i = 0 ; i < j ; i ++ ) { if ( arr [ j ] % arr [ i ] == 0 ) p ++ ; } for ( int k = j + 1 ; k < n ; k ++ ) { if ( arr [ k ] % arr [ j ] == 0 ) q ++ ; } count += p * q ; } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 2 } ; int N = arr . length ; System . out . println ( getCount ( arr , N ) ) ; } }