Count of permutations such that sum of K numbers from given range is even | Function to return the number of all permutations such that sum of K numbers in range is even ; Find total count of even and odd number in given range ; Iterate loop k times and update even_sum & odd_sum using previous values ; Update the prev_even and odd_sum ; Even sum ; Odd sum ; Return even_sum ; Given ranges ; Length of permutation ; Function call
function countEvenSum ( low , high , k ) { let even_count = high / 2 - ( low - 1 ) / 2 ; let odd_count = ( high + 1 ) / 2 - low / 2 ; let even_sum = 1 ; let odd_sum = 0 ; for ( let i = 0 ; i < k ; i ++ ) { let prev_even = even_sum ; let prev_odd = odd_sum ; even_sum = ( prev_even * even_count ) + ( prev_odd * odd_count ) ; odd_sum = ( prev_even * odd_count ) + ( prev_odd * even_count ) ; } document . write ( even_sum ) ; } let low = 4 ; let high = 5 ; let K = 3 ; countEvenSum ( low , high , K ) ;