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
def countKCountdown ( arr , N , K ) : NEW_LINE INDENT flag = - 1 ; NEW_LINE count = 0 ; NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT if ( arr [ i ] == K ) : NEW_LINE INDENT flag = K ; NEW_LINE DEDENT if ( arr [ i ] == flag ) : NEW_LINE INDENT flag -= 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT flag = - 1 ; NEW_LINE DEDENT if ( flag == 0 ) : NEW_LINE INDENT count += 1 ; NEW_LINE DEDENT DEDENT return count ; NEW_LINE DEDENT N = 8 ; NEW_LINE K = 3 ; NEW_LINE arr = [ 4 , 3 , 2 , 1 , 5 , 3 , 2 , 1 ] ; NEW_LINE print ( countKCountdown ( arr , N , K ) ) NEW_LINE