Count Strictly Increasing Subarrays
Last Updated :
18 Mar, 2025
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:
Input: arr[] = [1, 4, 5, 3, 7, 9]
Output: 6
Explanation: The strictly increasing subarrays are: [1, 4], [1, 4, 5], [4, 5], [3, 7], [3, 7, 9], [7, 9]
Input: arr[] = [1, 3, 3, 2, 3, 5]
Output: 4
Explanation: The strictly increasing subarrays are: [1, 3], [2, 3], [2, 3, 5], [3, 5]
Input: arr[] = [2, 2, 2, 2]
Output: 0
Explanation: No strictly increasing subarray exists.
[Naive Approach] Generate All Subarrays - O(n3) Time and O(1) Space
The idea is to iterate through all possible subarrays and check if they are strictly increasing. For each starting index, we expand the subarray and compare consecutive elements. If any element is not greater than the previous one, we stop checking further for that subarray. We increment the count for each valid subarray found.
C++
// C++ Code to count strictly increasing
// subarrays using Naive Approach
#include <bits/stdc++.h>
using namespace std;
// Function to count strictly increasing
// subarrays
int countIncreasing(vector<int>& arr) {
int n = arr.size();
int count = 0;
// Iterate over all possible subarrays
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
// Check if the current subarray is
// strictly increasing
bool isIncreasing = true;
for (int k = i; k < j; k++) {
if (arr[k] >= arr[k + 1]) {
isIncreasing = false;
break;
}
}
// If strictly increasing,
// increment count
if (isIncreasing) {
count++;
}
}
}
return count;
}
// Driver code
int main() {
vector<int> arr = {1, 4, 5, 3, 7, 9};
cout << countIncreasing(arr) << endl;
return 0;
}
Java
// Java Code to count strictly increasing
// subarrays using Naive Approach
class GfG {
// Function to count strictly increasing
// subarrays
static int countIncreasing(int[] arr) {
int n = arr.length;
int count = 0;
// Iterate over all possible subarrays
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
// Check if the current subarray is
// strictly increasing
boolean isIncreasing = true;
for (int k = i; k < j; k++) {
if (arr[k] >= arr[k + 1]) {
isIncreasing = false;
break;
}
}
// If strictly increasing,
// increment count
if (isIncreasing) {
count++;
}
}
}
return count;
}
// Driver code
public static void main(String[] args) {
int[] arr = {1, 4, 5, 3, 7, 9};
System.out.println(countIncreasing(arr));
}
}
Python
# Python Code to count strictly increasing
# subarrays using Naive Approach
# Function to count strictly increasing
# subarrays
def countIncreasing(arr):
n = len(arr)
count = 0
# Iterate over all possible subarrays
for i in range(n):
for j in range(i + 1, n):
# Check if the current subarray is
# strictly increasing
isIncreasing = True
for k in range(i, j):
if arr[k] >= arr[k + 1]:
isIncreasing = False
break
# If strictly increasing,
# increment count
if isIncreasing:
count += 1
return count
# Driver code
if __name__ == "__main__":
arr = [1, 4, 5, 3, 7, 9]
print(countIncreasing(arr))
C#
// C# Code to count strictly increasing
// subarrays using Naive Approach
using System;
class GfG {
// Function to count strictly increasing
// subarrays
public static int countIncreasing(int[] arr) {
int n = arr.Length;
int count = 0;
// Iterate over all possible subarrays
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
// Check if the current subarray is
// strictly increasing
bool isIncreasing = true;
for (int k = i; k < j; k++) {
if (arr[k] >= arr[k + 1]) {
isIncreasing = false;
break;
}
}
// If strictly increasing,
// increment count
if (isIncreasing) {
count++;
}
}
}
return count;
}
// Driver code
public static void Main() {
int[] arr = {1, 4, 5, 3, 7, 9};
Console.WriteLine(countIncreasing(arr));
}
}
JavaScript
// JavaScript Code to count strictly increasing
// subarrays using Naive Approach
// Function to count strictly increasing
// subarrays
function countIncreasing(arr) {
let n = arr.length;
let count = 0;
// Iterate over all possible subarrays
for (let i = 0; i < n; i++) {
for (let j = i + 1; j < n; j++) {
// Check if the current subarray is
// strictly increasing
let isIncreasing = true;
for (let k = i; k < j; k++) {
if (arr[k] >= arr[k + 1]) {
isIncreasing = false;
break;
}
}
// If strictly increasing,
// increment count
if (isIncreasing) {
count++;
}
}
}
return count;
}
// Driver code
let arr = [1, 4, 5, 3, 7, 9];
console.log(countIncreasing(arr));
Time Complexity: O(n³), iterates over all subarrays.
Space Complexity: O(1), uses only a few extra variables.
[Better Approach] Check All Subarrays using 2 Nested Loops - O(n2) Time and O(1) Space
The idea is to count all strictly increasing subarrays efficiently by expanding from each starting index i. Instead of checking every possible subarray, we extend j while the sequence remains increasing and stop immediately when it breaks.
C++
// C++ Code to count strictly increasing
// subarrays using Better Approach
#include <bits/stdc++.h>
using namespace std;
// Function to count strictly increasing
// subarrays
int countIncreasing(vector<int>& arr) {
int n = arr.size();
int count = 0;
// Iterate over all possible subarrays
for (int i = 0; i < n; i++) {
// Start from index i
for (int j = i + 1; j < n; j++) {
// If the sequence breaks, stop early
if (arr[j - 1] >= arr[j]) {
break;
}
// Otherwise, count the valid subarray
count++;
}
}
return count;
}
// Driver code
int main() {
vector<int> arr = {1, 4, 5, 3, 7, 9};
cout << countIncreasing(arr) << endl;
return 0;
}
Java
// Java Code to count strictly increasing
// subarrays using Better Approach
import java.util.*;
class GfG {
// Function to count strictly increasing
// subarrays
static int countIncreasing(int[] arr) {
int n = arr.length;
int count = 0;
// Iterate over all possible subarrays
for (int i = 0; i < n; i++) {
// Start from index i
for (int j = i + 1; j < n; j++) {
// If the sequence breaks, stop early
if (arr[j - 1] >= arr[j]) {
break;
}
// Otherwise, count the valid subarray
count++;
}
}
return count;
}
// Driver code
public static void main(String[] args) {
int[] arr = {1, 4, 5, 3, 7, 9};
System.out.println(countIncreasing(arr));
}
}
Python
# Python Code to count strictly increasing
# subarrays using Better Approach
# Function to count strictly increasing
# subarrays
def countIncreasing(arr):
n = len(arr)
count = 0
# Iterate over all possible subarrays
for i in range(n):
# Start from index i
for j in range(i + 1, n):
# If the sequence breaks, stop early
if arr[j - 1] >= arr[j]:
break
# Otherwise, count the valid subarray
count += 1
return count
# Driver code
if __name__ == "__main__":
arr = [1, 4, 5, 3, 7, 9]
print(countIncreasing(arr))
C#
// C# Code to count strictly increasing
// subarrays using Better Approach
using System;
class GfG {
// Function to count strictly increasing
// subarrays
public static int countIncreasing(int[] arr) {
int n = arr.Length;
int count = 0;
// Iterate over all possible subarrays
for (int i = 0; i < n; i++) {
// Start from index i
for (int j = i + 1; j < n; j++) {
// If the sequence breaks, stop early
if (arr[j - 1] >= arr[j]) {
break;
}
// Otherwise, count the valid subarray
count++;
}
}
return count;
}
// Driver code
public static void Main() {
int[] arr = {1, 4, 5, 3, 7, 9};
Console.WriteLine(countIncreasing(arr));
}
}
JavaScript
// JavaScript Code to count strictly increasing
// subarrays using Better Approach
// Function to count strictly increasing
// subarrays
function countIncreasing(arr) {
let n = arr.length;
let count = 0;
// Iterate over all possible subarrays
for (let i = 0; i < n; i++) {
// Start from index i
for (let j = i + 1; j < n; j++) {
// If the sequence breaks, stop early
if (arr[j - 1] >= arr[j]) {
break;
}
// Otherwise, count the valid subarray
count++;
}
}
return count;
}
// Driver code
let arr = [1, 4, 5, 3, 7, 9];
console.log(countIncreasing(arr));
Time Complexity: O(n²), due to two nested loops iterate over all subarrays.
Space Complexity: O(1), as no extra space is used except variables.
The idea is to count all strictly increasing subarrays efficiently by using a single pass through the array. Instead of checking every subarray explicitly, we track the length of increasing segments using len. When a decreasing element is encountered, we use the formula (len * (len - 1)) / 2 to count subarrays formed by the segment and then reset len. Finally, we add the remaining subarrays after the loop ends.
Steps to implement the above idea:
- Initialize count to store the number of strictly increasing subarrays and len to track the length of increasing sequences.
- Iterate through the array starting from index 1, comparing each element with its previous element to check for increasing order.
- If the current element is greater than the previous, increment len as it extends the increasing subarray.
- If the current element breaks the increasing sequence, update count using the formula (len*(len-1))/2 and reset len to 1.
- Continue iterating until the end of the array, applying the same logic for each increasing and non-increasing sequence.
- After the loop, add the remaining subarrays count using (len * (len - 1)) / 2 to include the last segment.
- Finally, return count, which holds the total number of strictly increasing subarrays in the given array.
Below is the implementation of the above approach:
C++
// C++ Code to count strictly increasing
// subarrays using Length based Formula
#include <bits/stdc++.h>
using namespace std;
// Function to count strictly increasing
// subarrays
int countIncreasing(vector<int>& arr) {
int n = arr.size();
int count = 0;
int len = 1;
// Iterate through the array
for (int i = 1; i < n; i++) {
// If current element is greater than
// previous, increase len
if (arr[i] > arr[i - 1]) {
len++;
}
else {
// Add subarrays count and reset len
count += (len * (len - 1)) / 2;
len = 1;
}
}
// Add remaining subarrays count
count += (len * (len - 1)) / 2;
return count;
}
// Driver code
int main() {
vector<int> arr = {1, 4, 5, 3, 7, 9};
cout << countIncreasing(arr) << endl;
return 0;
}
Java
// Java Code to count strictly increasing
// subarrays using Length based Formula
class GfG {
// Function to count strictly increasing
// subarrays
static int countIncreasing(int[] arr) {
int n = arr.length;
int count = 0;
int len = 1;
// Iterate through the array
for (int i = 1; i < n; i++) {
// If current element is greater than
// previous, increase len
if (arr[i] > arr[i - 1]) {
len++;
}
else {
// Add subarrays count and reset len
count += (len * (len - 1)) / 2;
len = 1;
}
}
// Add remaining subarrays count
count += (len * (len - 1)) / 2;
return count;
}
// Driver code
public static void main(String[] args) {
int[] arr = {1, 4, 5, 3, 7, 9};
System.out.println(countIncreasing(arr));
}
}
Python
# Python Code to count strictly increasing
# subarrays using Length based Formula
# Function to count strictly increasing
# subarrays
def countIncreasing(arr):
n = len(arr)
count = 0
len = 1
# Iterate through the array
for i in range(1, n):
# If current element is greater than
# previous, increase len
if arr[i] > arr[i - 1]:
len += 1
else:
# Add subarrays count and reset len
count += (len * (len - 1)) // 2
len = 1
# Add remaining subarrays count
count += (len * (len - 1)) // 2
return count
# Driver code
if __name__ == "__main__":
arr = [1, 4, 5, 3, 7, 9]
print(countIncreasing(arr))
C#
// C# Code to count strictly increasing
// subarrays using Length based Formula
using System;
class GfG {
// Function to count strictly increasing
// subarrays
static int countIncreasing(int[] arr) {
int n = arr.Length;
int count = 0;
int len = 1;
// Iterate through the array
for (int i = 1; i < n; i++) {
// If current element is greater than
// previous, increase len
if (arr[i] > arr[i - 1]) {
len++;
}
else {
// Add subarrays count and reset len
count += (len * (len - 1)) / 2;
len = 1;
}
}
// Add remaining subarrays count
count += (len * (len - 1)) / 2;
return count;
}
// Driver code
static void Main() {
int[] arr = {1, 4, 5, 3, 7, 9};
Console.WriteLine(countIncreasing(arr));
}
}
JavaScript
// JavaScript Code to count strictly increasing
// subarrays using Length based Formula
// Function to count strictly increasing
// subarrays
function countIncreasing(arr) {
let n = arr.length;
let count = 0;
let len = 1;
// Iterate through the array
for (let i = 1; i < n; i++) {
// If current element is greater than
// previous, increase len
if (arr[i] > arr[i - 1]) {
len++;
}
else {
// Add subarrays count and reset len
count += (len * (len - 1)) / 2;
len = 1;
}
}
// Add remaining subarrays count
count += (len * (len - 1)) / 2;
return count;
}
// Driver code
let arr = [1, 4, 5, 3, 7, 9];
console.log(countIncreasing(arr));
Time Complexity: O(n), due to single pass through arr[] ensures efficient computation.
Space Complexity: O(1), as only a few extra variables used, no additional storage.
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
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
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
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
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 Subarrays having Sum K
Given an unsorted array of integers, the task is to find the number of subarrays having a sum exactly equal to a given number k.Examples: Input : arr[] = [10, 2, -2, -20, 10], k = -10Output : 3Explanation: Subarrays: arr[0...3], arr[1...4], arr[3...4] have sum equal to -10.Input : arr[] = [9, 4, 20,
8 min read
Count of increasing substrings in given String
Given string str of length N, the task is to print the number of substrings in which every character's ASCII value is greater or equal to the ASCII value of the previous character. The substrings must at least be of length 2. Example: Input: str = "bcdabc"Output: 6Explanation: The 6 substrings with
7 min read
Counting inversions in an subarrays
Given an array arr[], the goal is to count the number of inversions in all the sub-arrays. An inversion is a pair of indices i and j such that i > j and arr[i] < arr[j]. A sub-array from index x to y ( x<= y) consists of the element's arr[x], arr[x+1], ..., arr[y]. The array arr[] can also
15+ min read
Count Subarrays With Exactly K Distinct Elements
Given an array arr[] and an integer k, the task is to find the count of subarrays such that each subarray has exactly k distinct elements.Examples:Input: arr[] = [1, 2, 2, 3], k = 2 Output: 4 Explanation: Subarrays with exactly 2 distinct elements are: [1, 2], [1, 2, 2] and [2, 3].Input: arr[] = [3,
10 min read
Count all increasing subsequences
Given an array arr[] of integers (values lie in range from 0 to 9). The task is to count the number of strictly increasing subsequences that can be formed from this array. Note: A strictly increasing subsequence is a sequence where each element is greater than the previous one, and the elements are
10 min read