Count the number of contiguous increasing and decreasing subsequences in a sequence
For a given distinct integer sequence of size N, the task is to count the number of contiguous increasing subsequence and contiguous decreasing subsequence in this sequence.
Examples:
Input: arr[] = { 80, 50, 60, 70, 40 }
Output: 1 2
Explanation:
The only one increasing subsequence is (50, 60, 70) and
two decreasing subsequences are (80, 50) and (70, 40).
Input: arr[] = { 10, 20, 23, 12, 5, 4, 61, 67, 87, 9 }
Output: 2 2
Explanation:
The increasing subsequences are (10, 20, 23) and (4, 61, 67, 87)
whereas the decreasing subsequences are (23, 12, 5, 4) and (87, 9).
Approach: The idea behind solving this problem is to use two arrays which keeps the track of increasing or decreasing indices based on next elements.
- Define two arrays max and min, such that the index of an element of the array is stored. For the first element of the array, if it is greater than its next element, it’s index is stored in the max array, else it is stored in the min array and so on.
- For the last element of the array, if it is greater than the previous element, it’s index is stored in the max array, else it is stored in min array.
- After this, all the maximal contiguous increasing subsequences are matched from (min to max), while all the maximal contiguous decreasing subsequences are matched from (max to min).
- Finally, if the index of the first element of array is stored in the first index of min array, then the number of maximal contiguous increasing subsequence possible is size of min array and maximal contiguous decreasing subsequences possible is (size of max array – 1).
- If the index of the first element of the array is stored in the first index of the max array, then the number of maximal contiguous increasing subsequence possible is(size of the max array -1) and maximal contiguous decreasing subsequences possible is the size of min array.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
void numOfSubseq( int arr[], int n)
{
int i, inc_count, dec_count;
int max[n], min[n];
int k1 = 0, k2 = 0;
if (arr[0] < arr[1])
min[k1++] = 0;
else
max[k2++] = 0;
for (i = 1; i < n - 1; i++) {
if (arr[i] < arr[i - 1] && arr[i] < arr[i + 1])
min[k1++] = i;
if (arr[i] > arr[i - 1] && arr[i] > arr[i + 1])
max[k2++] = i;
}
if (arr[n - 1] < arr[n - 2])
min[k1++] = n - 1;
else
max[k2++] = n - 1;
if (min[0] == 0) {
inc_count = k2;
dec_count = k1 - 1;
}
else {
inc_count = k2 - 1;
dec_count = k1;
}
cout << "Increasing Subsequence Count: " << inc_count
<< "\n" ;
cout << "Decreasing Subsequence Count: " << dec_count
<< "\n" ;
}
int main()
{
int arr[] = { 12, 8, 11, 13, 10, 15, 14, 16, 20 };
int n = sizeof (arr) / sizeof (arr[0]);
numOfSubseq(arr, n);
return 0;
}
|
Java
class GFG {
static void numOfSubseq( int arr[], int n)
{
int i, inc_count, dec_count;
int max[] = new int [n];
int min[] = new int [n];
int k1 = 0 , k2 = 0 ;
if (arr[ 0 ] < arr[ 1 ])
min[k1++] = 0 ;
else
max[k2++] = 0 ;
for (i = 1 ; i < n - 1 ; i++) {
if (arr[i] < arr[i - 1 ] && arr[i] < arr[i + 1 ])
min[k1++] = i;
if (arr[i] > arr[i - 1 ] && arr[i] > arr[i + 1 ])
max[k2++] = i;
}
if (arr[n - 1 ] < arr[n - 2 ])
min[k1++] = n - 1 ;
else
max[k2++] = n - 1 ;
if (min[ 0 ] == 0 ) {
inc_count = k2;
dec_count = k1 - 1 ;
}
else {
inc_count = k2 - 1 ;
dec_count = k1;
}
System.out.println( "Increasing Subsequence"
+ " Count: " + inc_count);
System.out.println( "Decreasing Subsequence"
+ " Count: " + dec_count);
}
public static void main(String[] args)
{
int arr[] = { 12 , 8 , 11 , 13 , 10 , 15 , 14 , 16 , 20 };
int n = arr.length;
numOfSubseq(arr, n);
}
}
|
Python3
def numOfSubseq(arr, n):
i, inc_count, dec_count = 0 , 0 , 0
max = [ 0 ] * n
min = [ 0 ] * n
k1 = 0
k2 = 0
if (arr[ 0 ] < arr[ 1 ]):
min [k1] = 0
k1 + = 1
else :
max [k2] = 0
k2 + = 1
for i in range ( 1 , n - 1 ):
if (arr[i] < arr[i - 1 ] and arr[i] < arr[i + 1 ]):
min [k1] = i
k1 + = 1
if (arr[i] > arr[i - 1 ] and arr[i] > arr[i + 1 ]):
max [k2] = i
k2 + = 1
if (arr[n - 1 ] < arr[n - 2 ]):
min [k1] = n - 1
k1 + = 1
else :
max [k2] = n - 1
k2 + = 1
if ( min [ 0 ] = = 0 ):
inc_count = k2
dec_count = k1 - 1
else :
inc_count = k2 - 1
dec_count = k1
print ( "Increasing Subsequence Count: " , inc_count)
print ( "Decreasing Subsequence Count: " , dec_count)
if __name__ = = '__main__' :
arr = [ 12 , 8 , 11 , 13 , 10 , 15 , 14 , 16 , 20 ]
n = len (arr)
numOfSubseq(arr, n)
|
C#
using System;
class GFG {
static void numOfSubseq( int [] arr, int n)
{
int i, inc_count, dec_count;
int [] max = new int [n];
int [] min = new int [n];
int k1 = 0, k2 = 0;
if (arr[0] < arr[1])
min[k1++] = 0;
else
max[k2++] = 0;
for (i = 1; i < n - 1; i++) {
if (arr[i] < arr[i - 1] && arr[i] < arr[i + 1])
min[k1++] = i;
if (arr[i] > arr[i - 1] && arr[i] > arr[i + 1])
max[k2++] = i;
}
if (arr[n - 1] < arr[n - 2])
min[k1++] = n - 1;
else
max[k2++] = n - 1;
if (min[0] == 0) {
inc_count = k2;
dec_count = k1 - 1;
}
else {
inc_count = k2 - 1;
dec_count = k1;
}
Console.WriteLine( "Increasing Subsequence"
+ " Count: " + inc_count);
Console.WriteLine( "Decreasing Subsequence"
+ " Count: " + dec_count);
}
static public void Main()
{
int [] arr = { 12, 8, 11, 13, 10, 15, 14, 16, 20 };
int n = arr.Length;
numOfSubseq(arr, n);
}
}
|
Javascript
<script>
function numOfSubseq(arr, n)
{
let i, inc_count, dec_count;
let max = new Array(n);
max.fill(0);
let min = new Array(n);
min.fill(0);
let k1 = 0, k2 = 0;
if (arr[0] < arr[1])
min[k1++] = 0;
else
max[k2++] = 0;
for (i = 1; i < n - 1; i++)
{
if (arr[i] < arr[i - 1]
&& arr[i] < arr[i + 1])
min[k1++] = i;
if (arr[i] > arr[i - 1]
&& arr[i] > arr[i + 1])
max[k2++] = i;
}
if (arr[n - 1] < arr[n - 2])
min[k1++] = n - 1;
else
max[k2++] = n - 1;
if (min[0] == 0)
{
inc_count = k2;
dec_count = k1 - 1;
}
else
{
inc_count = k2 - 1;
dec_count = k1;
}
document.write( "Increasing Subsequence" +
" Count: " + inc_count + "</br>" ) ;
document.write( "Decreasing Subsequence" +
" Count: " + dec_count + "</br>" ) ;
}
let arr = [ 12, 8, 11, 13, 10, 15, 14, 16, 20 ];
let n = arr.length;
numOfSubseq(arr, n);
</script>
|
Output:
Increasing Subsequence Count: 3
Decreasing Subsequence Count: 3
Time Complexity: O(N)
Auxiliary Space: O(N), where N is the size of the given array.
Last Updated :
26 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...