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
function getCount ( arr , n ) { var count = 0 ; for ( var j = 1 ; j < n - 1 ; j ++ ) { var p = 0 , q = 0 ; for ( var i = 0 ; i < j ; i ++ ) { if ( arr [ j ] % arr [ i ] == 0 ) p ++ ; } for ( var k = j + 1 ; k < n ; k ++ ) { if ( arr [ k ] % arr [ j ] == 0 ) q ++ ; } count += p * q ; } return count ; } var arr = [ 1 , 2 , 2 ] ; var N = arr . length ; document . write ( getCount ( arr , N ) ) ;