Count of K | C ++ code for the above program . ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countKCountdown ( int arr [ ] , int N , int K ) { int flag = -1 ; int count = 0 ; for ( int 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 ; } int main ( ) { int N = 8 ; int K = 3 ; int arr [ N ] = { 4 , 3 , 2 , 1 , 5 , 3 , 2 , 1 } ; cout << countKCountdown ( arr , N , K ) ; }