Counting the numbers whose flag are true .
let cnt = 0 ; for ( let i = 0 ; i < n ; ++ i ) { if ( f [ i ] ) { cnt ++ ; } } let NumberOfTriplets = ( cnt * ( cnt - 1 ) * ( cnt - 2 ) ) / 6 ; document . write ( NumberOfTriplets + " " + ans ) ; }