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++
// C++ program to count number of strictly
// increasing subarrays
#include<bits/stdc++.h>
usingnamespacestd;
intcountIncreasing(intarr[], intn)
{
// Initialize count of subarrays as 0
intcnt = 0;
// Pick starting point
for(inti=0; i<n; i++)
{
// Pick ending point
for(intj=i+1; j<n; j++)
{
if(arr[j] > arr[j-1])
cnt++;
// If subarray arr[i..j] is not strictly
// increasing, then subarrays after it , i.e.,
// arr[i..j+1], arr[i..j+2], .... cannot
// be strictly increasing
else
break;
}
}
returncnt;
}
// Driver program
intmain()
{
intarr[] = {1, 2, 2, 4};
intn = sizeof(arr)/sizeof(arr[0]);
cout << "Count of strictly increasing subarrays is "
<< countIncreasing(arr, n);
return0;
}
Java
// Java program to count number of strictly
// increasing subarrays
classTest
{
staticintarr[] = newint[]{1, 2, 2, 4};
staticintcountIncreasing(intn)
{
// Initialize count of subarrays as 0
intcnt = 0;
// Pick starting point
for(inti=0; i<n; i++)
{
// Pick ending point
for(intj=i+1; j<n; j++)
{
if(arr[j] > arr[j-1])
cnt++;
// If subarray arr[i..j] is not strictly
// increasing, then subarrays after it , i.e.,
// arr[i..j+1], arr[i..j+2], .... cannot
// be strictly increasing
else
break;
}
}
returncnt;
}
// Driver method to test the above function
publicstaticvoidmain(String[] args)
{
System.out.println("Count of strictly increasing subarrays is "+
countIncreasing(arr.length));
}
}
Python3
# Python3 program to count number
# of strictly increasing subarrays
defcountIncreasing(arr, n):
# Initialize count of subarrays as 0
cnt =0
# Pick starting point
fori inrange(0, n) :
# Pick ending point
forj inrange(i +1, n) :
ifarr[j] > arr[j -1] :
cnt +=1
# If subarray arr[i..j] is not strictly
# increasing, then subarrays after it , i.e.,
# arr[i..j+1], arr[i..j+2], .... cannot
# be strictly increasing
else:
break
returncnt
# Driver code
arr =[1, 2, 2, 4]
n =len(arr)
print("Count of strictly increasing subarrays is",
countIncreasing(arr, n))
# This code is contributed by Shreyanshi Arun.
C#
// C# program to count number of
// strictly increasing subarrays
usingSystem;
classTest
{
staticint[]arr = newint[]{1, 2, 2, 4};
staticintcountIncreasing(intn)
{
// Initialize count of subarrays as 0
intcnt = 0;
// Pick starting point
for(inti = 0; i < n; i++)
{
// Pick ending point
for(intj = i + 1; j < n; j++)
{
if(arr[j] > arr[j - 1])
cnt++;
// If subarray arr[i..j] is not strictly
// increasing, then subarrays after it ,
// i.e., arr[i..j+1], arr[i..j+2], ....
// cannot be strictly increasing
else
break;
}
}
returncnt;
}
// Driver Code
publicstaticvoidMain(String[] args)
{
Console.Write("Count of strictly increasing"+
"subarrays is "+ countIncreasing(arr.Length));
}
}
// This code is contributed by parashar.
PHP
<?php
// PHP program to count number of
// strictly increasing subarrays
functioncountIncreasing( $arr, $n)
{
// Initialize count of subarrays
// as 0
$cnt= 0;
// Pick starting point
for( $i= 0; $i< $n; $i++)
{
// Pick ending point
for( $j= $i+1; $j< $n; $j++)
{
if($arr[$j] > $arr[$j-1])
$cnt++;
// If subarray arr[i..j] is
// not strictly increasing,
// then subarrays after it,
// i.e., arr[i..j+1],
// arr[i..j+2], .... cannot
// be strictly increasing
else
break;
}
}
return$cnt;
}
// Driver program
$arr= array(1, 2, 2, 4);
$n= count($arr);
echo"Count of strictly increasing ",
"subarrays is ",
countIncreasing($arr, $n);
// This code is contributed by anuj_67.
?>
Javascript
<script>
// Javascript program to count number of strictly
// increasing subarrays
functioncountIncreasing(arr, n)
{
// Initialize count of subarrays as 0
let cnt = 0;
// Pick starting point
for(let i = 0; i < n; i++)
{
// Pick ending point
for(let j = i + 1; j < n; j++)
{
if(arr[j] > arr[j - 1])
cnt++;
// If subarray arr[i..j] is not strictly
// increasing, then subarrays after it , i.e.,
// arr[i..j+1], arr[i..j+2], .... cannot
// be strictly increasing
else
break;
}
}
returncnt;
}
// Driver code
let arr = [ 1, 2, 2, 4 ];
let n = arr.length;
document.write("Count of strictly "+
"increasing subarrays is "+
countIncreasing(arr, n));
// This code is contributed by divyesh072019
</script>
Output :
Count of strictly increasing subarrays is 2
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++
// C++ program to count number of strictly
// increasing subarrays in O(n) time.
#include<bits/stdc++.h>
usingnamespacestd;
intcountIncreasing(intarr[], intn)
{
intcnt = 0; // Initialize result
// Initialize length of current increasing
// subarray
intlen = 1;
// Traverse through the array
for(inti=0; i < n-1; ++i)
{
// If arr[i+1] is greater than arr[i],
// then increment length
if(arr[i + 1] > arr[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);
returncnt;
}
// Driver program
intmain()
{
intarr[] = {1, 2, 2, 4};
intn = sizeof(arr)/sizeof(arr[0]);
cout << "Count of strictly increasing subarrays is "
<< countIncreasing(arr, n);
return0;
}
Java
// Java program to count number of strictly
// increasing subarrays
classTest
{
staticintarr[] = newint[]{1, 2, 2, 4};
staticintcountIncreasing(intn)
{
intcnt = 0; // Initialize result
// Initialize length of current increasing
// subarray
intlen = 1;
// Traverse through the array
for(inti=0; i < n-1; ++i)
{
// If arr[i+1] is greater than arr[i],
// then increment length
if(arr[i + 1] > arr[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);
returncnt;
}
// Driver method to test the above function
publicstaticvoidmain(String[] args)
{
System.out.println("Count of strictly increasing subarrays is "+
countIncreasing(arr.length));
}
}
Python3
# Python3 program to count number of
# strictlyincreasing subarrays in O(n) time.
defcountIncreasing(arr, n):
cnt =0# Initialize result
# Initialize length of current
# increasing subarray
len=1
# Traverse through the array
fori inrange(0, n -1) :
# If arr[i+1] is greater than arr[i],
# then increment length
ifarr[i +1] > arr[i] :
len+=1
# Else Update count and reset length
else:
cnt +=(((len-1) *len) /2)
len=1
# If last length is more than 1
iflen> 1:
cnt +=(((len-1) *len) /2)
returncnt
# Driver program
arr =[1, 2, 2, 4]
n =len(arr)
print("Count of strictly increasing subarrays is",
int(countIncreasing(arr, n)))
# This code is contributed by Shreyanshi Arun.
C#
// C# program to count number of strictly
// increasing subarrays
usingSystem;
classGFG {
staticint[]arr = newint[]{1, 2, 2, 4};
staticintcountIncreasing(intn)
{
intcnt = 0; // Initialize result
// Initialize length of current
// increasing subarray
intlen = 1;
// Traverse through the array
for(inti = 0; i < n-1; ++i)
{
// If arr[i+1] is greater than
// arr[i], then increment length
if(arr[i + 1] > arr[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);
returncnt;
}
// Driver method to test the
// above function
publicstaticvoidMain()
{
Console.WriteLine("Count of strictly "
+ "increasing subarrays is "
+ countIncreasing(arr.Length));
}
}
// This code is contribute by anuj_67.
PHP
<?php
// PHP program to count number of strictly
// increasing subarrays in O(n) time.
functioncountIncreasing( $arr, $n)
{
// Initialize result
$cnt= 0;
// Initialize length of
// current increasing
// subarray
$len= 1;
// Traverse through the array
for($i= 0; $i< $n- 1; ++$i)
{
// If arr[i+1] is greater than arr[i],
// then increment length
if($arr[$i+ 1] > $arr[$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
$arr= array(1, 2, 2, 4);
$n= count($arr);
echo"Count of strictly increasing subarrays is "
, countIncreasing($arr, $n);
// This code is contribute by anuj_67
?>
Javascript
<script>
// Javascript program to count number of strictly
// increasing subarrays
let arr = [1, 2, 2, 4];
functioncountIncreasing(n)
{
let cnt = 0; // Initialize result
// Initialize length of current
// increasing subarray
let len = 1;
// Traverse through the array
for(let i = 0; i < n-1; ++i)
{
// If arr[i+1] is greater than
// arr[i], then increment length
if(arr[i + 1] > arr[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);
returncnt;
}
document.write("Count of strictly "
+ "increasing subarrays is "
+ countIncreasing(arr.length));
</script>
Output :
Count of strictly increasing subarrays is 2
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy