Find the count of Strictly decreasing Subarrays
Last Updated :
08 Sep, 2022
Given an array A[] of integers. The task is to count the total number of strictly decreasing subarrays( with size > 1 ).
Examples:
Input : A[] = { 100, 3, 1, 15 }
Output : 3
Subarrays are -> { 100, 3 }, { 100, 3, 1 }, { 3, 1 }
Input : A[] = { 4, 2, 2, 1 }
Output : 2
Naive Approach: A simple solution is to run two for loops and check whether the subarray is decreasing or not.
This can be improved by knowing the fact that if subarray arr[i:j] is not strictly decreasing, then subarrays arr[i:j+1], arr[i:j+2], .. arr[i:n-1] cannot be strictly decreasing.
Efficient Approach: In the above solution, we count many subarrays twice. This can also be improved and the idea is based on fact that a sorted(decreasing) subarray of length ‘len’ adds len*(len-1)/2 to the result.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countDecreasing( int A[], int n)
{
int cnt = 0;
int len = 1;
for ( int i = 0; i < n - 1; ++i) {
if (A[i + 1] < A[i])
len++;
else {
cnt += (((len - 1) * len) / 2);
len = 1;
}
}
if (len > 1)
cnt += (((len - 1) * len) / 2);
return cnt;
}
int main()
{
int A[] = { 100, 3, 1, 13 };
int n = sizeof (A) / sizeof (A[0]);
cout << countDecreasing(A, n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int countDecreasing( int A[], int n)
{
int cnt = 0 ;
int len = 1 ;
for ( int i = 0 ; i < n - 1 ; ++i) {
if (A[i + 1 ] < A[i])
len++;
else {
cnt += (((len - 1 ) * len) / 2 );
len = 1 ;
}
}
if (len > 1 )
cnt += (((len - 1 ) * len) / 2 );
return cnt;
}
public static void main (String[] args) {
int A[] = { 100 , 3 , 1 , 13 };
int n = A.length;
System.out.println(countDecreasing(A, n));
}
}
|
Python 3
def countDecreasing(A, n):
cnt = 0
len = 1
for i in range (n - 1 ) :
if (A[i + 1 ] < A[i]):
len + = 1
else :
cnt + = ((( len - 1 ) * len ) / / 2 );
len = 1
if ( len > 1 ):
cnt + = ((( len - 1 ) * len ) / / 2 )
return cnt
if __name__ = = "__main__" :
A = [ 100 , 3 , 1 , 13 ]
n = len (A)
print (countDecreasing(A, n))
|
C#
using System;
class GFG
{
static int countDecreasing( int []A, int n)
{
int cnt = 0;
int len = 1;
for ( int i = 0; i < n - 1; ++i) {
if (A[i + 1] < A[i])
len++;
else {
cnt += (((len - 1) * len) / 2);
len = 1;
}
}
if (len > 1)
cnt += (((len - 1) * len) / 2);
return cnt;
}
static void Main()
{
int []A = { 100, 3, 1, 13 };
int n = A.Length ;
Console.WriteLine(countDecreasing(A, n));
}
}
|
PHP
<?php
function countDecreasing( $A , $n )
{
$cnt = 0;
$len = 1;
for ( $i = 0; $i < $n - 1; ++ $i )
{
if ( $A [ $i + 1] < $A [ $i ])
$len ++;
else
{
$cnt += ((( $len - 1) * $len ) / 2);
$len = 1;
}
}
if ( $len > 1)
$cnt += ((( $len - 1) * $len ) / 2);
return $cnt ;
}
$A = array ( 100, 3, 1, 13 );
$n = sizeof( $A );
echo countDecreasing( $A , $n );
?>
|
Javascript
<script>
function countDecreasing(A, n)
{
var cnt = 0;
var len = 1;
for ( var i = 0; i < n - 1; ++i) {
if (A[i + 1] < A[i])
len++;
else {
cnt += parseInt(((len - 1) * len) / 2);
len = 1;
}
}
if (len > 1)
cnt += parseInt(((len - 1) * len) / 2);
return cnt;
}
var A = [ 100, 3, 1, 13 ];
var n = A.length;
document.write( countDecreasing(A, n));
</script>
|
Complexity Analysis:
- Time Complexity: O(N)
- Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...