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 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 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 result.

Below is the implementation of above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to count number of strictly
// decreasing subarrays in O(n) time.
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to count the number of strictly
// decreasing subarrays
int countDecreasing(int A[], int n)
{
    int cnt = 0; // Initialize result
  
    // Initialize length of current
    // decreasing subarray
    int len = 1;
  
    // Traverse through the array
    for (int i = 0; i < n - 1; ++i) {
        // If arr[i+1] is less than arr[i],
        // then increment length
        if (A[i + 1] < A[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 program
int main()
{
    int A[] = { 100, 3, 1, 13 };
    int n = sizeof(A) / sizeof(A[0]);
  
    cout << countDecreasing(A, n);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to count number of strictly
// decreasing subarrays in O(n) time.
  
import java.io.*;
  
class GFG {
  
  
// Function to count the number of strictly
// decreasing subarrays
static int countDecreasing(int A[], int n)
{
    int cnt = 0; // Initialize result
  
    // Initialize length of current
    // decreasing subarray
    int len = 1;
  
    // Traverse through the array
    for (int i = 0; i < n - 1; ++i) {
        // If arr[i+1] is less than arr[i],
        // then increment length
        if (A[i + 1] < A[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 program
    public static void main (String[] args) {
    int A[] = { 100, 3, 1, 13 };
    int n = A.length;
  
    System.out.println(countDecreasing(A, n));
    }
}
// This code is contributed by anuj_67..

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 program to count number 
# of strictly decreasing subarrays
# in O(n) time.
  
# Function to count the number of
# strictly decreasing subarrays
def countDecreasing(A, n):
  
    cnt = 0 # Initialize result
  
    # Initialize length of current
    # decreasing subarray
    len = 1
  
    # Traverse through the array
    for i in range (n - 1) :
          
        # If arr[i+1] is less than arr[i],
        # then increment length
        if (A[i + 1] < A[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 cnt
  
# Driver Code
if __name__=="__main__":
  
    A = [ 100, 3, 1, 13 ]
    n = len(A)
  
    print (countDecreasing(A, n))
  
# This code is contributed by ChitraNayal

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to count number of strictly
// decreasing subarrays in O(n) time.
  
using System;
  
class GFG
{
// Function to count the number of strictly
// decreasing subarrays
static int countDecreasing(int []A, int n)
{
int cnt = 0; // Initialize result
  
// Initialize length of current
// decreasing subarray
int len = 1;
  
// Traverse through the array
for (int i = 0; i < n - 1; ++i) {
// If arr[i+1] is less than arr[i],
// then increment length
if (A[i + 1] < A[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
static void Main()
{
int []A = { 100, 3, 1, 13 };
int n = A.Length ;
Console.WriteLine(countDecreasing(A, n));
}
// This code is contributed by ANKITRAI1
}

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to count number of strictly
// decreasing subarrays in O(n) time.
  
// Function to count the number of 
// strictly decreasing subarrays
function countDecreasing($A, $n)
{
    $cnt = 0; // Initialize result
  
    // Initialize length of current
    // decreasing subarray
    $len = 1;
  
    // Traverse through the array
    for ($i = 0; $i < $n - 1; ++$i
    {
        // If arr[i+1] is less than arr[i],
        // then increment length
        if ($A[$i + 1] < $A[$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
$A = array( 100, 3, 1, 13 );
$n = sizeof($A);
  
echo countDecreasing($A, $n);
  
// This code is contributed by mits
?>

chevron_right


Output:

3

Time Complexity: O(N)



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.