Open In App

Count number of strictly increasing and decreasing subarrays

Last Updated : 11 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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 8
Explanation: The increasing subarrays are: {80}, {50}, {60}, {70}, {40}, {40}, {50, 60}, {60, 70} and {50, 60, 70}. The decreasing subarrays are: {80}, {50}, {60}, {70}, {40}, {40}, {80, 50} and {70, 40}.

Input: arr[] = { 10, 20, 23, 12, 5, 4, 61, 67, 87, 9 } 
Output: 2 2 

Approach: To solve the problem, follow the below idea:

The problem can be solved in two parts: count the number of strictly increasing subarrays and count the number of strictly decreasing subarrays. The problem can be solved using two pointers technique, start and end to mark the starting and ending of the subarray respectively.

We know that if an array of length L is strictly increasing, then all the subarrays inside this array will also be increasing. So, there will be a total of (L * (L + 1)) / 2 strictly increasing subarrays. So using two pointers, we keep on incrementing end to find the strictly increasing subarray and as soon as the subarray starts to decrease, we add count of all subarrays between start and end.

Similarly, if a array of length L is strictly is strictly decreasing, then all the subarrays inside this array will also be decreasing. So, there will be a total of (L * (L + 1)) / 2 strictly decreasing subarrays. So using two pointers, we keep on incrementing end to find the strictly decreasing subarray and as soon as the subarray starts to increase, we add count of all subarrays between start and end.

Below is the implementation of the above approach: 

C++
// C++ Program to find the number of strictly increasing and
// decreasing subarrays

#include <bits/stdc++.h>
using namespace std;

// function to find the count of increasing subarrays
int countIncreasing(int arr[], int n)
{
  	// Initialize result
    int cnt = 0; 

    // Initialize start and end pointers
    int start = 0, end = 1;

    // Traverse through the array
    while (end < n) {
        if (arr[end] <= arr[end - 1]) {
            int len = end - start;
            cnt += (len * (len + 1)) / 2;
            start = end;
        }
        end++;
    }

    // Count the last remaining subarrays
    int len = end - start;
    cnt += ((len * (len + 1)) / 2);

    return cnt;
}

// function to find the count of decreasing subarrays
int countDecreasing(int arr[], int n)
{
    int cnt = 0; // Initialize result

    // Initialize start and end pointers
    int start = 0, end = 1;

    // Traverse through the array
    while (end < n) {
        if (arr[end] >= arr[end - 1]) {
            int len = end - start;
            cnt += (len * (len + 1)) / 2;
            start = end;
        }
        end++;
    }

    // Count the last remaining subarrays
    int len = end - start;
    cnt += ((len * (len + 1)) / 2);

    return cnt;
}

// Driver program
int main()
{
    int arr[] = { 80, 50, 60, 70, 40, 40 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << "Count of strictly increasing subarrays is "
         << countIncreasing(arr, n) << endl;
    cout << "Count of strictly decreasing subarrays is "
         << countDecreasing(arr, n) << endl;
    return 0;
}
Java
// Java Program to find the number of strictly increasing
// and decreasing subarrays

public class GFG {

    // Function to find the count of strictly increasing
    // subarrays
    public static int countIncreasing(int[] arr)
    {
        // Initialize result
        int cnt = 0;

        // Initialize start and end pointers
        int start = 0, end = 1;

        // Traverse through the array
        while (end < arr.length) {
            if (arr[end] <= arr[end - 1]) {
                // Calculate the length of the increasing
                // subarray
                int len = end - start;
                // Add the number of increasing subarrays
                cnt += (len * (len + 1)) / 2;
                // Update the start pointer
                start = end;
            }
            end++;
        }

        // Count the last remaining subarrays
        int len = end - start;
        cnt += ((len * (len + 1)) / 2);

        return cnt;
    }

    // Function to find the count of strictly decreasing
    // subarrays
    public static int countDecreasing(int[] arr)
    {
        int cnt = 0; // Initialize result

        // Initialize start and end pointers
        int start = 0, end = 1;

        // Traverse through the array
        while (end < arr.length) {
            if (arr[end] >= arr[end - 1]) {
                // Calculate the length of the decreasing
                // subarray
                int len = end - start;
                // Add the number of decreasing subarrays
                cnt += (len * (len + 1)) / 2;
                // Update the start pointer
                start = end;
            }
            end++;
        }

        // Count the last remaining subarrays
        int len = end - start;
        cnt += ((len * (len + 1)) / 2);

        return cnt;
    }

    // Driver code
    public static void main(String[] args)
    {
        int[] arr = { 80, 50, 60, 70, 40, 40 };
        System.out.println(
            "Count of strictly increasing subarrays is "
            + countIncreasing(arr));
        System.out.println(
            "Count of strictly decreasing subarrays is "
            + countDecreasing(arr));
    }
}
Python
# Python Program to find the number of strictly increasing
# and decreasing subarrays

# Function to find the count of strictly increasing subarrays
def count_increasing(arr):
    # Initialize result
    cnt = 0

    # Initialize start and end pointers
    start = 0
    end = 1

    # Traverse through the array
    while end < len(arr):
        if arr[end] <= arr[end - 1]:
            # Calculate the length of the increasing subarray
            length = end - start
            # Add the number of increasing subarrays
            cnt += (length * (length + 1)) // 2
            # Update the start pointer
            start = end
        end += 1

    # Count the last remaining subarrays
    length = end - start
    cnt += ((length * (length + 1)) // 2)

    return cnt

# Function to find the count of strictly decreasing subarrays
def count_decreasing(arr):
    # Initialize result
    cnt = 0

    # Initialize start and end pointers
    start = 0
    end = 1

    # Traverse through the array
    while end < len(arr):
        if arr[end] >= arr[end - 1]:
            # Calculate the length of the decreasing subarray
            length = end - start
            # Add the number of decreasing subarrays
            cnt += (length * (length + 1)) // 2
            # Update the start pointer
            start = end
        end += 1

    # Count the last remaining subarrays
    length = end - start
    cnt += ((length * (length + 1)) // 2)

    return cnt

# Driver code
arr = [80, 50, 60, 70, 40, 40]
print("Count of strictly increasing subarrays is", count_increasing(arr))
print("Count of strictly decreasing subarrays is", count_decreasing(arr))
C#
using System;

class GFG {
    // Function to find the count of strictly increasing
    // subarrays
    static int CountIncreasing(int[] arr)
    {
        // Initialize result
        int cnt = 0;

        // Initialize start and end pointers
        int start = 0;
        int end = 1;

        // Traverse through the array
        while (end < arr.Length) {
            if (arr[end] <= arr[end - 1]) {
                // Calculate the length of the increasing
                // subarray
                int len = end - start;

                // Add the number of increasing subarrays of
                // the current length
                cnt += (len * (len + 1)) / 2;

                // Update the start pointer for the next
                // increasing subarray
                start = end;
            }
            end++;
        }

        // Count the last remaining subarrays
        int remainingLen = end - start;
        cnt += (remainingLen * (remainingLen + 1)) / 2;

        return cnt;
    }

    // Function to find the count of strictly decreasing
    // subarrays
    static int CountDecreasing(int[] arr)
    {
        // Initialize result
        int cnt = 0;

        // Initialize start and end pointers
        int start = 0;
        int end = 1;

        // Traverse through the array
        while (end < arr.Length) {
            if (arr[end] >= arr[end - 1]) {
                // Calculate the length of the decreasing
                // subarray
                int len = end - start;

                // Add the number of decreasing subarrays of
                // the current length
                cnt += (len * (len + 1)) / 2;

                // Update the start pointer for the next
                // decreasing subarray
                start = end;
            }
            end++;
        }

        // Count the last remaining subarrays
        int remainingLen = end - start;
        cnt += (remainingLen * (remainingLen + 1)) / 2;

        return cnt;
    }

    // Main method to drive the program
    static void Main()
    {
        int[] arr = { 80, 50, 60, 70, 40, 40 };

        // Function call to count strictly increasing
        // subarrays
        int increasingCount = CountIncreasing(arr);
        Console.WriteLine(
            "Count of strictly increasing subarrays is "
            + increasingCount);

        // Function call to count strictly decreasing
        // subarrays
        int decreasingCount = CountDecreasing(arr);
        Console.WriteLine(
            "Count of strictly decreasing subarrays is "
            + decreasingCount);
    }
}
JavaScript
// Function to find the count of strictly increasing subarrays
function countIncreasing(arr) {
    let cnt = 0; // Initialize result
    let start = 0;
    let end = 1;

    // Traverse through the array
    while (end < arr.length) {
        if (arr[end] <= arr[end - 1]) {
            // Calculate the length of the increasing subarray
            let len = end - start;
            // Add the number of increasing subarrays of the current length
            cnt += (len * (len + 1)) / 2;
            // Update the start pointer for the next increasing subarray
            start = end;
        }
        end++;
    }

    // Count the last remaining subarrays
    let len = end - start;
    cnt += (len * (len + 1)) / 2;

    return cnt;
}

// Function to find the count of strictly decreasing subarrays
function countDecreasing(arr) {
    let cnt = 0; // Initialize result
    let start = 0;
    let end = 1;

    // Traverse through the array
    while (end < arr.length) {
        if (arr[end] >= arr[end - 1]) {
            // Calculate the length of the decreasing subarray
            let len = end - start;
            // Add the number of decreasing subarrays of the current length
            cnt += (len * (len + 1)) / 2;
            // Update the start pointer for the next decreasing subarray
            start = end;
        }
        end++;
    }

    // Count the last remaining subarrays
    let len = end - start;
    cnt += (len * (len + 1)) / 2;

    return cnt;
}

// Driver function
function main() {
    const arr = [80, 50, 60, 70, 40, 40];
    const increasingCount = countIncreasing(arr);
    const decreasingCount = countDecreasing(arr);

    console.log("Count of strictly increasing subarrays is " + increasingCount);
    console.log("Count of strictly decreasing subarrays is " + decreasingCount);
}

// Call the main function to execute the program
main();

Output
Count of strictly increasing subarrays is 9
Count of strictly decreasing subarrays is 8

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


Next Article

Similar Reads