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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


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#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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
?>

chevron_right


Output:

8   

Time Complexity: O(N)
Auxiliary Space: O(1)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.