Count the number of non-increasing subarrays
Last Updated :
09 Jun, 2022
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++
// C++ program to count number of non
// increasing subarrays
#include <bits/stdc++.h>
using namespace std;
int countNonIncreasing(int arr[], int n)
{
// Initialize result
int cnt = 0;
// Initialize length of current non
// increasing subarray
int len = 1;
// Traverse through the array
for (int i = 0; i < n - 1; ++i) {
// If arr[i+1] is less than or equal to arr[i],
// then increment length
if (arr[i + 1] <= arr[i])
len++;
// Else Update count and reset length
else {
cnt += (((len + 1) * len) / 2);
len = 1;
}
}
// If last length is more than 1
if (len > 1)
cnt += (((len + 1) * len) / 2);
return cnt;
}
// Driver code
int main()
{
int arr[] = { 5, 2, 3, 7, 1, 1 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << countNonIncreasing(arr, n);
return 0;
}
Java
// Java program to count number of non
// increasing subarrays
class GFG
{
static int countNonIncreasing(int arr[], int n)
{
// Initialize result
int cnt = 0;
// Initialize length of current non
// increasing subarray
int len = 1;
// Traverse through the array
for (int i = 0; i < n - 1; ++i)
{
// If arr[i+1] is less than or equal to arr[i],
// then increment length
if (arr[i + 1] <= arr[i])
len++;
// Else Update count and reset length
else
{
cnt += (((len + 1) * len) / 2);
len = 1;
}
}
// If last length is more than 1
if (len > 1)
cnt += (((len + 1) * len) / 2);
return cnt;
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 5, 2, 3, 7, 1, 1 };
int n =arr.length;
System.out.println(countNonIncreasing(arr, n));
}
}
// This code is contributed by Code_Mech
Python3
# Python3 program to count number of non
# increasing subarrays
def countNonIncreasing(arr, n):
# Initialize result
cnt = 0;
# Initialize length of current
# non-increasing subarray
len = 1;
# Traverse through the array
for i in range(0, n - 1):
# If arr[i+1] is less than
# or equal to arr[i],
# then increment length
if (arr[i + 1] <= arr[i]):
len += 1;
# Else Update count and reset length
else:
cnt += (((len + 1) * len) / 2);
len = 1;
# If last length is more than 1
if (len > 1):
cnt += (((len + 1) * len) / 2);
return int(cnt);
# Driver code
if __name__ == '__main__':
arr = [5, 2, 3, 7, 1, 1];
n = len(arr);
print(countNonIncreasing(arr, n));
# This code contributed by PrinciRaj1992
C#
// C# program to count number of non
// increasing subarrays
using System;
class GFG
{
static int countNonIncreasing(int []arr, int n)
{
// Initialize result
int cnt = 0;
// Initialize length of current non
// increasing subarray
int len = 1;
// Traverse through the array
for (int i = 0; i < n - 1; ++i)
{
// If arr[i+1] is less than or equal to arr[i],
// then increment length
if (arr[i + 1] <= arr[i])
len++;
// Else Update count and reset length
else
{
cnt += (((len + 1) * len) / 2);
len = 1;
}
}
// If last length is more than 1
if (len > 1)
cnt += (((len + 1) * len) / 2);
return cnt;
}
// Driver code
public static void Main(String[] args)
{
int []arr = { 5, 2, 3, 7, 1, 1 };
int n = arr.Length;
Console.Write(countNonIncreasing(arr, n));
}
}
// This code has been contributed by 29AjayKumar
PHP
<?php
// PHP program to count number of non
// increasing subarrays
function countNonIncreasing($arr, $n)
{
// Initialize result
$cnt = 0;
// Initialize length of current non
// increasing subarray
$len = 1;
// Traverse through the array
for ($i = 0; $i < $n - 1; ++$i)
{
// If arr[i+1] is less than
// or equal to arr[i],
// then increment length
if ($arr[$i + 1] <= $arr[$i])
$len++;
// Else Update count and reset length
else
{
$cnt += (($len + 1) * $len) / 2;
$len = 1;
}
}
// If last length is more than 1
if ($len > 1)
$cnt += (($len + 1) * $len) / 2;
return $cnt;
}
// Driver code
$arr = array( 5, 2, 3, 7, 1, 1 );
$n = sizeof($arr);
echo countNonIncreasing($arr, $n);
// This code is contributed by akt_mit
?>
JavaScript
<script>
// Javascript program to count number of non
// increasing subarrays
function countNonIncreasing(arr, n)
{
// Initialize result
var cnt = 0;
// Initialize length of current non
// increasing subarray
var len = 1;
// Traverse through the array
for(var i = 0; i < n - 1; ++i)
{
// If arr[i+1] is less than or equal
// to arr[i], then increment length
if (arr[i + 1] <= arr[i])
len++;
// Else Update count and reset length
else
{
cnt += parseInt(((len + 1) * len) / 2);
len = 1;
}
}
// If last length is more than 1
if (len > 1)
cnt += parseInt(((len + 1) * len) / 2);
return cnt;
}
// Driver code
var arr = [ 5, 2, 3, 7, 1, 1 ];
var n = arr.length;
document.write(countNonIncreasing(arr, n));
// This code is contributed by rutvik_56
</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.
Similar Reads
Count number of increasing sub-sequences : O(NlogN) Given an array arr[] of length N, the task is to find the number of strictly increasing sub-sequences in the given array.Examples: Input: arr[] = {1, 2, 3} Output: 7 All increasing sub-sequences will be: 1) {1} 2) {2} 3) {3} 4) {1, 2} 5) {1, 3} 6) {2, 3} 7) {1, 2, 3} Thus, answer = 7Input: arr[] = {
12 min read
Find the count of Strictly decreasing Subarrays 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 so
6 min read
Count the number of valid contiguous Subarrays Given an array, arr[] consisting of N integers and integer K, the task is to count the number of contiguous subarrays where each element is in the subarray at least K times. Examples: Input: N = 3, arr[] = {0, 0, 0}, K = 2Output: 3Explanation: [0], [0], [0] - 0[0, 0], [0, 0] - 2[0, 0, 0] - 1total =
6 min read
Count Strictly Increasing Subarrays Given an integer array arr[], the task is to count the number of subarrays in arr[] that are strictly increasing and have a size of at least 2. A subarray is a contiguous sequence of elements from arr[]. A subarray is strictly increasing if each element is greater than its previous element.Examples:
13 min read
Count number of strictly increasing and decreasing subarrays Given an array arr[] of size N, the task is to find the number of strictly increasing and decreasing subarrays.Examples: Input: arr[] = { 80, 50, 60, 70, 40, 40 } Output: 9 8Explanation: The increasing subarrays are: {80}, {50}, {60}, {70}, {40}, {40}, {50, 60}, {60, 70} and {50, 60, 70}. The decrea
9 min read