Count of K | Java 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
class GFG { public static 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 ; } public static void main ( String [ ] args ) { int N = 8 ; int K = 3 ; int arr [ ] = { 4 , 3 , 2 , 1 , 5 , 3 , 2 , 1 } ; System . out . print ( countKCountdown ( arr , N , K ) ) ; } }