Count subarrays consisting of first K natural numbers in descending order
Last Updated :
26 Sep, 2023
Given an array arr[] of size N and an integer K, the task is to count the number of subarrays which consists of first K natural numbers in descending order.
Examples:
Input: arr[] = {1, 2, 3, 7, 9, 3, 2, 1, 8, 3, 2, 1}, K = 3
Output: 2
Explanation: The subarray {3, 2, 1} occurs twice in the array.
Input: arr = {100, 7, 6, 5, 4, 3, 2, 1, 100}, K = 6
Output: 1
Naive Approach
The idea is to find all subarrays and then find those subarrays whose length is equal to k. After that from those subarrays find the number of subarrays that consist of the first K natural number in decreasing order.
Steps to implement-
- Initialize a variable ans with value 0 to store the final answer
- Run two for loops to find all subarray
- Simultaneously find the length of the subarray
- If any subarray has a length of K
- Then check whether it contains the first K natural numbers in descending order
- If Yes then increment the ans by 1
Code-
C++
#include <bits/stdc++.h>
using namespace std;
int CountSubarray( int arr[], int n,
int k)
{
int ans=0;
for ( int i=0;i<n;i++){
int length=0;
for ( int j=i;j<n;j++){
length++;
if (length==k){
int count=k;
int m=i;
while (m<=j){
if (arr[m]==count){count--;}
else { break ;}
m++;
}
if (count==0){ans++;}
}
}
}
return ans;
}
int main()
{
int arr[] = { 1, 2, 3, 7, 9, 3,
2, 1, 8, 3, 2, 1 };
int N = sizeof (arr) / sizeof (arr[0]);
int K = 3;
cout << CountSubarray(arr, N, K);
return 0;
}
|
Java
import java.util.*;
public class GFG {
static int countSubarray( int [] arr, int n, int k) {
int ans = 0 ;
for ( int i = 0 ; i < n; i++) {
int length = 0 ;
for ( int j = i; j < n; j++) {
length++;
if (length == k) {
int count = k;
int m = i;
while (m <= j) {
if (arr[m] == count) {
count--;
} else {
break ;
}
m++;
}
if (count == 0 ) {
ans++;
}
}
}
}
return ans;
}
public static void main(String[] args) {
int [] arr = { 1 , 2 , 3 , 7 , 9 , 3 , 2 , 1 , 8 , 3 , 2 , 1 };
int N = arr.length;
int K = 3 ;
System.out.println(countSubarray(arr, N, K));
}
}
|
Python3
def count_subarray(arr, n, k):
ans = 0
for i in range (n):
length = 0
for j in range (i, n):
length + = 1
if length = = k:
count = k
m = i
while m < = j:
if arr[m] = = count:
count - = 1
else :
break
m + = 1
if count = = 0 :
ans + = 1
return ans
if __name__ = = "__main__" :
arr = [ 1 , 2 , 3 , 7 , 9 , 3 , 2 , 1 , 8 , 3 , 2 , 1 ]
N = len (arr)
K = 3
print (count_subarray(arr, N, K))
|
C#
using System;
class GFG {
static int CountSubarray( int [] arr, int n, int k)
{
int ans = 0;
for ( int i = 0; i < n; i++) {
int length = 0;
for ( int j = i; j < n; j++) {
length++;
if (length == k) {
int count = k;
int m = i;
while (m <= j) {
if (arr[m] == count) {
count--;
}
else {
break ;
}
m++;
}
if (count == 0) {
ans++;
}
}
}
}
return ans;
}
static public void Main()
{
int [] arr = { 1, 2, 3, 7, 9, 3, 2, 1, 8, 3, 2, 1 };
int N = arr.Length;
int K = 3;
Console.Write(CountSubarray(arr, N, K));
}
}
|
Javascript
function CountSubarray(arr, n, k) {
let ans = 0;
for (let i = 0; i < n; i++) {
let length = 0;
for (let j = i; j < n; j++) {
length++;
if (length === k) {
let count = k;
let m = i;
while (m <= j) {
if (arr[m] === count) {
count--;
} else {
break ;
}
m++;
}
if (count === 0) {
ans++;
}
}
}
}
return ans;
}
const arr = [1, 2, 3, 7, 9, 3, 2, 1, 8, 3, 2, 1];
const N = arr.length;
const K = 3;
console.log(CountSubarray(arr, N, K));
|
Time Complexity: O(N3), because of two loops to find all subarray and a third loop to choose subarray which consists of first K natural numbers in descending order
Auxiliary Space: O(1), because no extra space has been used
Approach: The idea is to traverse the array and check if the required decreasing sequence is present starting from the current index or not. Follow the steps below to solve the problem:
- Initialize two variables, temp to K, that checks the pattern, and count with 0, to store the count of total subarray matched.
- Traverse the array arr[] using the variable i and do the following:
- If arr[i] is equal to temp and the value of temp is 1, then increment the count by 1 and update temp as K. Else decrement temp by 1.
- Otherwise, update temp as temp = K and if arr[i] is equal to K, decrement i by 1.
- After the above steps, print the value of count as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int CountSubarray( int arr[], int n,
int k)
{
int temp = k, count = 0;
for ( int i = 0; i < n; i++) {
if (arr[i] == temp) {
if (temp == 1) {
count++;
temp = k;
}
else
temp--;
}
else {
temp = k;
if (arr[i] == k)
i--;
}
}
return count;
}
int main()
{
int arr[] = { 1, 2, 3, 7, 9, 3,
2, 1, 8, 3, 2, 1 };
int N = sizeof (arr) / sizeof (arr[0]);
int K = 3;
cout << CountSubarray(arr, N, K);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int CountSubarray( int arr[], int n,
int k)
{
int temp = k, count = 0 ;
for ( int i = 0 ; i < n; i++) {
if (arr[i] == temp) {
if (temp == 1 ) {
count++;
temp = k;
}
else
temp--;
}
else {
temp = k;
if (arr[i] == k)
i--;
}
}
return count;
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 3 , 7 , 9 , 3 ,
2 , 1 , 8 , 3 , 2 , 1 };
int N = arr.length;
int K = 3 ;
System.out.println(CountSubarray(arr, N, K));
}
}
|
Python3
def CountSubarray(arr, n, k):
temp = k
count = 0
for i in range (n):
if (arr[i] = = temp):
if (temp = = 1 ):
count + = 1
temp = k
else :
temp - = 1
else :
temp = k
if (arr[i] = = k):
i - = 1
return count
if __name__ = = "__main__" :
arr = [ 1 , 2 , 3 , 7 , 9 , 3 ,
2 , 1 , 8 , 3 , 2 , 1 ]
N = len (arr)
K = 3
print (CountSubarray(arr, N, K))
|
C#
using System;
class GFG{
static int CountSubarray( int [] arr,
int n, int k)
{
int temp = k, count = 0;
for ( int i = 0; i < n; i++)
{
if (arr[i] == temp)
{
if (temp == 1)
{
count++;
temp = k;
}
else
temp--;
}
else
{
temp = k;
if (arr[i] == k)
i--;
}
}
return count;
}
static public void Main()
{
int [] arr = { 1, 2, 3, 7, 9, 3,
2, 1, 8, 3, 2, 1 };
int N = arr.Length;
int K = 3;
Console.Write(CountSubarray(arr, N, K));
}
}
|
Javascript
<script>
function CountSubarray(arr, n, k) {
var temp = k,
count = 0;
for ( var i = 0; i < n; i++)
{
if (arr[i] == temp)
{
if (temp == 1) {
count++;
temp = k;
} else temp--;
}
else {
temp = k;
if (arr[i] == k) i--;
}
}
return count;
}
var arr = [1, 2, 3, 7, 9, 3, 2, 1, 8, 3, 2, 1];
var N = arr.length;
var K = 3;
document.write(CountSubarray(arr, N, K));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...