Count of even sum triplets in the array for Q range queries | Function to count number of triplets with even sum in range l , r for each query ; Initialization of array ; Initialization of variables ; Traversing array ; If element is odd ; If element is even ; Storing count of even and odd till each i ; Traversing each query ; Count of odd numbers in l to r ; Count of even numbers in l to r ; Finding the ans ; Prleting the ans ; Given Input ; Function Call
function countTriplets ( size , queries , arr , Q ) { let arr_even = new Array ( size + 1 ) ; let arr_odd = new Array ( size + 1 ) ; let even = 0 ; let odd = 0 ; arr_even [ 0 ] = 0 ; arr_odd [ 0 ] = 0 ; for ( let i = 0 ; i < size ; i ++ ) { if ( arr [ i ] % 2 == 1 ) { odd ++ ; } else { even ++ ; } arr_even [ i + 1 ] = even ; arr_odd [ i + 1 ] = odd ; } for ( let i = 0 ; i < queries ; i ++ ) { let l = Q [ i ] [ 0 ] , r = Q [ i ] [ 1 ] ; odd = arr_odd [ r ] - arr_odd [ l - 1 ] ; even = arr_even [ r ] - arr_even [ l - 1 ] ; let ans = ( even * ( even - 1 ) * ( even - 2 ) ) / 6 + ( odd * ( odd - 1 ) / 2 ) * even ; document . write ( ans + " " ) ; } } let N = 6 , Q = 2 ; let arr = [ 1 , 2 , 3 , 4 , 5 , 6 ] ; let queries = [ [ 1 , 3 ] , [ 2 , 5 ] ] ; countTriplets ( N , Q , arr , queries ) ;