Count the number of non-increasing subarrays
Given an array of N integers. The task is to count the number of subarrays (of size at least one) that are non-increasing.
Examples:
Input : arr[] = {1, 4, 3}
Output : 4
The possible subarrays are {1}, {4}, {3}, {4, 3}.
Input :{4, 3, 2, 1}
Output : 10
The possible subarrays are:
{4}, {3}, {2}, {1}, {4, 3}, {3, 2}, {2, 1},
{4, 3, 2}, {3, 2, 1}, {4, 3, 2, 1}.
Simple Solution: A simple solution is to generate all possible subarrays, and for every subarray check if the subarray is non increasing or not. The time complexity of this solution would be O(N3).
Efficient Solution: A Better Solution is to use the fact that if a subarray arr[i:j] is not non increasing, then subarrays arr[i:j+1], arr[i:j+2], .. arr[i:n-1] cannot be non increasing. So, start traversing the array and for current subarray keep incrementing it’s length until it is non-increasing and update the count. Once the subarray starts increasing reset the length.
Below is the implementation of the above idea:
C++
#include <bits/stdc++.h>
using namespace std;
int countNonIncreasing( int arr[], int n)
{
int cnt = 0;
int len = 1;
for ( int i = 0; i < n - 1; ++i) {
if (arr[i + 1] <= arr[i])
len++;
else {
cnt += (((len + 1) * len) / 2);
len = 1;
}
}
if (len > 1)
cnt += (((len + 1) * len) / 2);
return cnt;
}
int main()
{
int arr[] = { 5, 2, 3, 7, 1, 1 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << countNonIncreasing(arr, n);
return 0;
}
|
Java
class GFG
{
static int countNonIncreasing( int arr[], int n)
{
int cnt = 0 ;
int len = 1 ;
for ( int i = 0 ; i < n - 1 ; ++i)
{
if (arr[i + 1 ] <= arr[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 arr[] = { 5 , 2 , 3 , 7 , 1 , 1 };
int n =arr.length;
System.out.println(countNonIncreasing(arr, n));
}
}
|
Python3
def countNonIncreasing(arr, n):
cnt = 0 ;
len = 1 ;
for i in range ( 0 , n - 1 ):
if (arr[i + 1 ] < = arr[i]):
len + = 1 ;
else :
cnt + = ((( len + 1 ) * len ) / 2 );
len = 1 ;
if ( len > 1 ):
cnt + = ((( len + 1 ) * len ) / 2 );
return int (cnt);
if __name__ = = '__main__' :
arr = [ 5 , 2 , 3 , 7 , 1 , 1 ];
n = len (arr);
print (countNonIncreasing(arr, n));
|
C#
using System;
class GFG
{
static int countNonIncreasing( int []arr, int n)
{
int cnt = 0;
int len = 1;
for ( int i = 0; i < n - 1; ++i)
{
if (arr[i + 1] <= arr[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 []arr = { 5, 2, 3, 7, 1, 1 };
int n = arr.Length;
Console.Write(countNonIncreasing(arr, n));
}
}
|
PHP
<?php
function countNonIncreasing( $arr , $n )
{
$cnt = 0;
$len = 1;
for ( $i = 0; $i < $n - 1; ++ $i )
{
if ( $arr [ $i + 1] <= $arr [ $i ])
$len ++;
else
{
$cnt += (( $len + 1) * $len ) / 2;
$len = 1;
}
}
if ( $len > 1)
$cnt += (( $len + 1) * $len ) / 2;
return $cnt ;
}
$arr = array ( 5, 2, 3, 7, 1, 1 );
$n = sizeof( $arr );
echo countNonIncreasing( $arr , $n );
?>
|
Javascript
<script>
function countNonIncreasing(arr, n)
{
var cnt = 0;
var len = 1;
for ( var i = 0; i < n - 1; ++i)
{
if (arr[i + 1] <= arr[i])
len++;
else
{
cnt += parseInt(((len + 1) * len) / 2);
len = 1;
}
}
if (len > 1)
cnt += parseInt(((len + 1) * len) / 2);
return cnt;
}
var arr = [ 5, 2, 3, 7, 1, 1 ];
var n = arr.length;
document.write(countNonIncreasing(arr, n));
</script>
|
Time Complexity: O(N), since there runs a loop from 0 to (n – 2).
Auxiliary Space: O(1), since no extra space has been taken.
Last Updated :
09 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...