Count Strictly Increasing Subarrays
Given an array of integers, count number of subarrays (of size more than one) that are strictly increasing.
Expected Time Complexity : O(n)
Expected Extra Space: O(1)
Examples:
Input: arr[] = {1, 4, 3}
Output: 1
There is only one subarray {1, 4}
Input: arr[] = {1, 2, 3, 4}
Output: 6
There are 6 subarrays {1, 2}, {1, 2, 3}, {1, 2, 3, 4}
{2, 3}, {2, 3, 4} and {3, 4}
Input: arr[] = {1, 2, 2, 4}
Output: 2
There are 2 subarrays {1, 2} and {2, 4}
A Simple Solution is to generate all possible subarrays, and for every subarray check if subarray is strictly increasing or not. Worst case time complexity of this solution would be O(n3).
A Better Solution is to use the fact that if subarray arr[i:j] is not strictly increasing, then subarrays arr[i:j+1], arr[i:j+2], .. arr[i:n-1] cannot be strictly increasing. Below is the program based on above idea.
C++
#include<bits/stdc++.h>
using namespace std;
int countIncreasing( int arr[], int n)
{
int cnt = 0;
for ( int i=0; i<n; i++)
{
for ( int j=i+1; j<n; j++)
{
if (arr[j] > arr[j-1])
cnt++;
else
break ;
}
}
return cnt;
}
int main()
{
int arr[] = {1, 2, 2, 4};
int n = sizeof (arr)/ sizeof (arr[0]);
cout << "Count of strictly increasing subarrays is "
<< countIncreasing(arr, n);
return 0;
}
|
Java
class Test
{
static int arr[] = new int []{ 1 , 2 , 2 , 4 };
static int countIncreasing( int n)
{
int cnt = 0 ;
for ( int i= 0 ; i<n; i++)
{
for ( int j=i+ 1 ; j<n; j++)
{
if (arr[j] > arr[j- 1 ])
cnt++;
else
break ;
}
}
return cnt;
}
public static void main(String[] args)
{
System.out.println( "Count of strictly increasing subarrays is " +
countIncreasing(arr.length));
}
}
|
Python3
def countIncreasing(arr, n):
cnt = 0
for i in range ( 0 , n) :
for j in range (i + 1 , n) :
if arr[j] > arr[j - 1 ] :
cnt + = 1
else :
break
return cnt
arr = [ 1 , 2 , 2 , 4 ]
n = len (arr)
print ( "Count of strictly increasing subarrays is" ,
countIncreasing(arr, n))
|
C#
using System;
class Test
{
static int []arr = new int []{1, 2, 2, 4};
static int countIncreasing( int n)
{
int cnt = 0;
for ( int i = 0; i < n; i++)
{
for ( int j = i + 1; j < n; j++)
{
if (arr[j] > arr[j - 1])
cnt++;
else
break ;
}
}
return cnt;
}
public static void Main(String[] args)
{
Console.Write( "Count of strictly increasing" +
"subarrays is " + countIncreasing(arr.Length));
}
}
|
PHP
<?php
function countIncreasing( $arr , $n )
{
$cnt = 0;
for ( $i = 0; $i < $n ; $i ++)
{
for ( $j = $i +1; $j < $n ; $j ++)
{
if ( $arr [ $j ] > $arr [ $j -1])
$cnt ++;
else
break ;
}
}
return $cnt ;
}
$arr = array (1, 2, 2, 4);
$n = count ( $arr );
echo "Count of strictly increasing " ,
"subarrays is " ,
countIncreasing( $arr , $n );
?>
|
Javascript
<script>
function countIncreasing(arr, n)
{
let cnt = 0;
for (let i = 0; i < n; i++)
{
for (let j = i + 1; j < n; j++)
{
if (arr[j] > arr[j - 1])
cnt++;
else
break ;
}
}
return cnt;
}
let arr = [ 1, 2, 2, 4 ];
let n = arr.length;
document.write( "Count of strictly " +
"increasing subarrays is " +
countIncreasing(arr, n));
</script>
|
Output :
Count of strictly increasing subarrays is 2
Time Complexity: O(n2)
Auxiliary Space: O(1)
Time complexity of the above solution is O(m) where m is number of subarrays in output
This problem and solution are contributed by Rahul Agrawal.
An Efficient Solution can count subarrays in O(n) time. The idea is based on fact that a sorted subarray of length ‘len’ adds len*(len-1)/2 to result. For example, {10, 20, 30, 40} adds 6 to the result.
C++
#include<bits/stdc++.h>
using namespace std;
int countIncreasing( int arr[], int n)
{
int cnt = 0;
int len = 1;
for ( int i=0; i < n-1; ++i)
{
if (arr[i + 1] > arr[i])
len++;
else
{
cnt += (((len - 1) * len) / 2);
len = 1;
}
}
if (len > 1)
cnt += (((len - 1) * len) / 2);
return cnt;
}
int main()
{
int arr[] = {1, 2, 2, 4};
int n = sizeof (arr)/ sizeof (arr[0]);
cout << "Count of strictly increasing subarrays is "
<< countIncreasing(arr, n);
return 0;
}
|
Java
class Test
{
static int arr[] = new int []{ 1 , 2 , 2 , 4 };
static int countIncreasing( int n)
{
int cnt = 0 ;
int len = 1 ;
for ( int i= 0 ; i < n- 1 ; ++i)
{
if (arr[i + 1 ] > arr[i])
len++;
else
{
cnt += (((len - 1 ) * len) / 2 );
len = 1 ;
}
}
if (len > 1 )
cnt += (((len - 1 ) * len) / 2 );
return cnt;
}
public static void main(String[] args)
{
System.out.println( "Count of strictly increasing subarrays is " +
countIncreasing(arr.length));
}
}
|
Python3
def countIncreasing(arr, n):
cnt = 0
len = 1
for i in range ( 0 , n - 1 ) :
if arr[i + 1 ] > arr[i] :
len + = 1
else :
cnt + = ((( len - 1 ) * len ) / 2 )
len = 1
if len > 1 :
cnt + = ((( len - 1 ) * len ) / 2 )
return cnt
arr = [ 1 , 2 , 2 , 4 ]
n = len (arr)
print ( "Count of strictly increasing subarrays is" ,
int (countIncreasing(arr, n)))
|
C#
using System;
class GFG {
static int []arr = new int []{1, 2, 2, 4};
static int countIncreasing( int n)
{
int cnt = 0;
int len = 1;
for ( int i = 0; i < n-1; ++i)
{
if (arr[i + 1] > arr[i])
len++;
else
{
cnt += (((len - 1) * len) / 2);
len = 1;
}
}
if (len > 1)
cnt += (((len - 1) * len) / 2);
return cnt;
}
public static void Main()
{
Console.WriteLine( "Count of strictly "
+ "increasing subarrays is "
+ countIncreasing(arr.Length));
}
}
|
PHP
<?php
function countIncreasing( $arr , $n )
{
$cnt = 0;
$len = 1;
for ( $i = 0; $i < $n - 1; ++ $i )
{
if ( $arr [ $i + 1] > $arr [ $i ])
$len ++;
else
{
$cnt += ((( $len - 1) * $len ) / 2);
$len = 1;
}
}
if ( $len > 1)
$cnt += ((( $len - 1) * $len ) / 2);
return $cnt ;
}
$arr = array (1, 2, 2, 4);
$n = count ( $arr );
echo "Count of strictly increasing subarrays is "
, countIncreasing( $arr , $n );
?>
|
Javascript
<script>
let arr = [1, 2, 2, 4];
function countIncreasing(n)
{
let cnt = 0;
let len = 1;
for (let i = 0; i < n-1; ++i)
{
if (arr[i + 1] > arr[i])
len++;
else
{
cnt += (((len - 1) * len) / 2);
len = 1;
}
}
if (len > 1)
cnt += (((len - 1) * len) / 2);
return cnt;
}
document.write( "Count of strictly "
+ "increasing subarrays is "
+ countIncreasing(arr.length));
</script>
|
Output :
Count of strictly increasing subarrays is 2
Time Complexity: O(n)
Auxiliary Space: O(1)
Last Updated :
18 Sep, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...