Count number of strictly increasing and decreasing subarrays
Last Updated :
11 Feb, 2025
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();
OutputCount of strictly increasing subarrays is 9
Count of strictly decreasing subarrays is 8
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
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 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 non decreasing subarrays of size N from N Natural numbers
Given are N natural numbers, the task is to find the count of the subarrays of size N that can be formed using elements from 1 to N such that each element in the subarray is smaller than or equal to the elements to its right (a[i] ? a[i+1]).Examples: Input: N = 2 Output: 3 Explanation: Given array o
5 min read
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},
6 min read
Split an array into minimum number of non-increasing or non-decreasing subarrays
Given an array arr[] of size N, the task is to split the given array into a minimum number of subarrays such that elements of each subarray are either in non-increasing order or non-decreasing order. Examples: Input: arr[] = {2, 3, 9, 5, 4, 6, 8}Output: 3Explanation: Split the array into 3 subarrays
10 min read
Find Maximum Sum Strictly Increasing Subarray
Given an array of positive integers. Find the maximum sum of strictly increasing subarrays. Note that this problem is different from maximum subarray sum and maximum sum increasing subsequence problems. Examples: Input : arr[] = {1, 2, 3, 2, 5, 1, 7}Output : 8Explanation : Some Strictly increasing s
7 min read
Minimum number of strictly decreasing subsequences
Given an array arr[] of size N. The task is to split the array into minimum number of strictly decreasing subsequences. Calculate the minimum number of subsequences we can get by splitting. Examples: Input: N = 4, arr[] = {3, 5, 1, 2}Output: 2Explanation: We can split the array into two subsequences
7 min read
Count Subarrays with strictly decreasing consecutive elements
Given an array arr[] containing integers. The task is to find the number of decreasing subarrays with a difference of 1. Examples: Input: arr[] = {3, 2, 1, 4}Output: 7Explanation: Following are the possible decreasing subarrays with difference 1. [3], [2], [1], [4], [3,2], [2,1], and [3,2,1]Therefor
7 min read
Count of strictly increasing and decreasing paths in directed Binary Tree
Given a directed binary tree of N nodes whose edges are directed from the parent to the child, the task is to count the number of strictly increasing and decreasing paths. Note: A path starts at the root and ends at any leaf. Examples: Input: N = 6tree = 6 / \ 4 7 \ / \ 5 6 8Output: 10 8Explanation:
12 min read
Differences between number of increasing subarrays and decreasing subarrays in k sized windows
Given an array of integers and k, find the difference between the number of the strictly increasing number of subarrays (of size more than one) and the number of the strictly decreasing subarray in the window of size k. Examples: Input : nums = {10, 20, 30, 15, 15}; k = 3; Output : 3, 0, -1 Explanat
8 min read