Count of K-countdowns in an Array
Last Updated :
20 May, 2021
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++
#include <bits/stdc++.h>
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);
}
|
Java
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));
}
}
|
Python3
def countKCountdown(arr, N, K):
flag = - 1 ;
count = 0 ;
for i in range ( 0 , N):
if (arr[i] = = K):
flag = K;
if (arr[i] = = flag):
flag - = 1 ;
else :
flag = - 1 ;
if (flag = = 0 ):
count + = 1 ;
return count;
N = 8 ;
K = 3 ;
arr = [ 4 , 3 , 2 , 1 ,
5 , 3 , 2 , 1 ];
print (countKCountdown(arr, N, K))
|
C#
using System;
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()
{
int N = 8;
int K = 3;
int []arr = { 4, 3, 2, 1, 5, 3, 2, 1 };
Console.Write(countKCountdown(arr, N, K));
}
}
|
Javascript
<script>
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));
</script>
|
Time Complexity: O(N)
Auxiliary Space Complexity: O(1)
Share your thoughts in the comments
Please Login to comment...