Count of K | Function to to count the number of K - countdowns for multiple queries ; flag which stores the current value of value in the countdown ; count of K - countdowns ; Loop to iterate over the elements of the array ; condition check if the elements of the array is equal to K ; condition check if the elements of the array is in continuous order ; condition check if the elements of the array are not in continuous order ; condition check to increment the counter if the there is a K - countdown present in the array ; returning the count of K - countdowns ; Driver code ; Function Call
function countKCountdown ( arr , N , K ) { var flag = - 1 ; var count = 0 ; for ( var i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == K ) flag = K ; if ( arr [ i ] == flag ) flag -- ; else flag = - 1 ; if ( flag == 0 ) count ++ ; } return count ; } var N = 8 ; var K = 3 ; var arr = [ 4 , 3 , 2 , 1 , 5 , 3 , 2 , 1 ] ; document . write ( countKCountdown ( arr , N , K ) ) ;