Count of K-countdowns in an Array

Given an array arr[] of length N and a number K, the task is to count the number of K-countdowns in the array.

A contiguous subarray is said to be a K-countdown if it is of length K and contains the integers K, K-1, K-2, …, 2, 1 in that order. For example, [4, 3, 2, 1] is 4-countdown and [6, 5, 4, 3, 2, 1] is a 6-countdown.

Examples:

Input: K = 2, arr[] = {3 2 1 2 2 1}
Output: 2
Explanation: Here, K=2 so the array has 2 2-Countdowns(2, 1). One countdown is from index 1 to 2 and the other is from index 4 to 5.

Input: K = 3, arr[] = {4 3 2 1 5 3 2 1}
Output: 2
Explanation: Here, K=3 so the array has 2 3-Countdowns(3, 2, 1)



Approach: The given array is traversed and every time the number K is encountered, it is checked if all the numbers K, K-1, K-2, … up to 1 are sequentially present in the array or not. If yes, the count is increased by 1. If the next number takes it out of sequence, then the next occurrence of K is looked for.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ code for the above program.
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to to count the
// number of K-countdowns for
// multiple queries
int countKCountdown(int arr[],
                    int N,
                    int K)
{
  
    // flag which stores the
    // current value of value
    // in the countdown
    int flag = -1;
  
    // count of K-countdowns
    int count = 0;
  
    // Loop to iterate over the
    // elements of the array
    for (int i = 0; i < N; i++) {
  
        // condition check if
        // the elements
        // of the array is
        // equal to K
        if (arr[i] == K)
            flag = K;
  
        // condition check if
        // the elements
        // of the array is in
        // continuous order
        if (arr[i] == flag)
            flag--;
  
        // condition check if
        // the elements
        // of the array are not
        // in continuous order
        else
            flag = -1;
  
        // condition check to
        // increment the counter
        // if the there is a
        // K-countdown present
        // in the array
        if (flag == 0)
            count++;
    }
  
    // returning the count of
    // K-countdowns
    return count;
}
  
// Driver Code
int main()
{
    int N = 8;
    int K = 3;
    int arr[N] = { 4, 3, 2, 1,
                   5, 3, 2, 1 };
  
    // Function Call
    cout << countKCountdown(arr, N, K);
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code for the above program.
class GFG{
      
// Function to to count the 
// number of K-countdowns for 
// multiple queries 
public static int countKCountdown(int arr[], 
                                  int N, int K) 
      
    // Flag which stores the 
    // current value of value 
    // in the countdown 
    int flag = -1
      
    // Count of K-countdowns 
    int count = 0
      
    // Loop to iterate over the 
    // elements of the array 
    for(int i = 0; i < N; i++) 
    {
         
       // Condition check if the
       // elements of the array is 
       // equal to K 
       if (arr[i] == K) 
           flag = K; 
             
       // Condition check if the
       // elements of the array is 
       // in continuous order 
       if (arr[i] == flag) 
           flag--; 
         
       // Condition check if the
       // elements of the array are 
       // not in continuous order 
       else
           flag = -1
         
       // Condition check to increment 
       // the counter if the there is a 
       // K-countdown present in the array 
       if (flag == 0
           count++; 
    
      
    // Returning the count of 
    // K-countdowns 
    return count; 
  
// Driver code
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));
}
}
  
// This code is contributed by divyeshrabadiya07

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code for the above program.
  
# Function to to count the
# number of K-countdowns for
# multiple queries
def countKCountdown(arr, N, K):
  
    # flag which stores the
    # current value of value
    # in the countdown
    flag = -1;
  
    # count of K-countdowns
    count = 0;
  
    # Loop to iterate over the
    # elements of the array
    for i in range(0, N):
  
        # condition check if
        # the elements
        # of the array is
        # equal to K
        if (arr[i] == K):
            flag = K;
  
        # condition check if
        # the elements
        # of the array is in
        # continuous order
        if (arr[i] == flag):
            flag -= 1;
  
        # condition check if
        # the elements
        # of the array are not
        # in continuous order
        else:
            flag = -1;
  
        # condition check to
        # increment the counter
        # if the there is a
        # K-countdown present
        # in the array
        if (flag == 0):
            count += 1;
      
    # returning the count of
    # K-countdowns
    return count;
  
# Driver Code
N = 8;
K = 3;
arr = [ 4, 3, 2, 1,
        5, 3, 2, 1 ];
  
# Function Call
print(countKCountdown(arr, N, K))
  
# This code is contributed by Akanksha_Rai

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code for the above program.
using System;
class GFG{
      
// Function to to count the 
// number of K-countdowns for 
// multiple queries 
public static int countKCountdown(int []arr, 
                                  int N, int K) 
      
    // Flag which stores the 
    // current value of value 
    // in the countdown 
    int flag = -1; 
      
    // Count of K-countdowns 
    int count = 0; 
      
    // Loop to iterate over the 
    // elements of the array 
    for(int i = 0; i < N; i++) 
    {
          
    // Condition check if the
    // elements of the array is 
    // equal to K 
    if (arr[i] == K) 
        flag = K; 
              
    // Condition check if the
    // elements of the array is 
    // in continuous order 
    if (arr[i] == flag) 
        flag--; 
          
    // Condition check if the
    // elements of the array are 
    // not in continuous order 
    else
        flag = -1; 
          
    // Condition check to increment 
    // the counter if the there is a 
    // K-countdown present in the array 
    if (flag == 0) 
        count++; 
    
      
    // Returning the count of 
    // K-countdowns 
    return count; 
  
// Driver code
public static void Main() 
{
    int N = 8; 
    int K = 3; 
    int []arr = { 4, 3, 2, 1, 5, 3, 2, 1 }; 
      
    Console.Write(countKCountdown(arr, N, K));
}
}
  
// This code is contributed by Akanksha_Rai

chevron_right


Output:

2


Time Complexity: O(N)
Auxiliary Space Complexity: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.