text: Number of quadruples where the first three terms are in AP and last three terms are in GP | Function to return the count of quadruples ; Hash table to count the number of occurrences ; Traverse and increment the count ; Run two nested loop for second and third element ; If they are same ; Initially decrease the count ; Find the first element using common difference ; Find the fourth element using GP y ^ 2 = x * z property ; If it is an leteger ; If not equal ; Same elements ; Later increase the value for future calculations ; Driver code
code: function countQuadruples ( a , n ) { let mp = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) if ( mp . has ( a [ i ] ) ) { mp . set ( a [ i ] , mp . get ( a [ i ] ) + 1 ) ; } else { mp . set ( a [ i ] , 1 ) ; } let count = 0 ; for ( let j = 0 ; j < n ; j ++ ) { for ( let k = 0 ; k < n ; k ++ ) { if ( j == k ) continue ; mp . set ( a [ j ] , mp . get ( a [ j ] ) - 1 ) ; mp . set ( a [ k ] , mp . get ( a [ k ] ) - 1 ) ; let first = a [ j ] - ( a [ k ] - a [ j ] ) ; let fourth = ( a [ k ] * a [ k ] ) / a [ j ] ; if ( ( a [ k ] * a [ k ] ) % a [ j ] == 0 ) { if ( a [ j ] != a [ k ] ) { if ( mp . has ( first ) && mp . has ( fourth ) ) count += mp . get ( first ) * mp . get ( fourth ) ; } else if ( mp . has ( first ) && mp . has ( fourth ) ) count += mp . get ( first ) * ( mp . get ( fourth ) - 1 ) ; } if ( mp . has ( a [ j ] ) ) { mp . set ( a [ j ] , mp . get ( a [ j ] ) + 1 ) ; } else { mp . set ( a [ j ] , 1 ) ; } if ( mp . has ( a [ k ] ) ) { mp . set ( a [ k ] , mp . get ( a [ k ] ) + 1 ) ; } else { mp . set ( a [ k ] , 1 ) ; } } } return count ; } let a = [ 2 , 6 , 4 , 9 , 2 ] ; let n = a . length ; document . write ( countQuadruples ( a , n ) ) ;