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++
// C++ implementation of the above approach. #include <bits/stdc++.h> using namespace std; // Function to count the number of maximal contiguous // increasing and decreasing subsequences void numOfSubseq( int arr[], int n) { int i, inc_count, dec_count; int max[n], min[n]; // k2, k1 are used to store the // count of max and min array int k1 = 0, k2 = 0; // Comparison to store the index of // first element of array if (arr[0] < arr[1]) min[k1++] = 0; else max[k2++] = 0; // Comparison to store the index // from second to second last // index of array 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; } // Comparison to store the index // of last element of array if (arr[n - 1] < arr[n - 2]) min[k1++] = n - 1; else max[k2++] = n - 1; // Count of number of maximal contiguous // increasing and decreasing subsequences 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" ; } // Driver program to test above 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
// Java implementation of the above approach. class GFG { // Function to count the number of maximal contiguous // increasing and decreasing subsequences static void numOfSubseq( int arr[], int n) { int i, inc_count, dec_count; int max[] = new int [n]; int min[] = new int [n]; // k2, k1 are used to store the // count of max and min array int k1 = 0 , k2 = 0 ; // Comparison to store the index of // first element of array if (arr[ 0 ] < arr[ 1 ]) min[k1++] = 0 ; else max[k2++] = 0 ; // Comparison to store the index // from second to second last // index of array 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; } // Comparison to store the index // of last element of array if (arr[n - 1 ] < arr[n - 2 ]) min[k1++] = n - 1 ; else max[k2++] = n - 1 ; // Count of number of maximal contiguous // increasing and decreasing subsequences 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); } // Driver code public static void main(String[] args) { int arr[] = { 12 , 8 , 11 , 13 , 10 , 15 , 14 , 16 , 20 }; int n = arr.length; numOfSubseq(arr, n); } } // This code is contributed by AnkitRai01 |
Python3
# Python3 implementation of the above approach. # Function to count the number of maximal contiguous # increasing and decreasing subsequences def numOfSubseq(arr, n): i, inc_count, dec_count = 0 , 0 , 0 max = [ 0 ] * n min = [ 0 ] * n # k2, k1 are used to store the # count of max and min array k1 = 0 k2 = 0 # Comparison to store the index of # first element of array if (arr[ 0 ] < arr[ 1 ]): min [k1] = 0 k1 + = 1 else : max [k2] = 0 k2 + = 1 # Comparison to store the index # from second to second last # index of array 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 # Comparison to store the index # of last element of array if (arr[n - 1 ] < arr[n - 2 ]): min [k1] = n - 1 k1 + = 1 else : max [k2] = n - 1 k2 + = 1 # Count of number of maximal contiguous # increasing and decreasing subsequences 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) # Driver code if __name__ = = '__main__' : arr = [ 12 , 8 , 11 , 13 , 10 , 15 , 14 , 16 , 20 ] n = len (arr) numOfSubseq(arr, n) # This code is contributed by 29AjayKumar |
C#
// C# implementation of the above approach. using System; class GFG { // Function to count the number of maximal contiguous // increasing and decreasing subsequences static void numOfSubseq( int [] arr, int n) { int i, inc_count, dec_count; int [] max = new int [n]; int [] min = new int [n]; // k2, k1 are used to store the // count of max and min array int k1 = 0, k2 = 0; // Comparison to store the index of // first element of array if (arr[0] < arr[1]) min[k1++] = 0; else max[k2++] = 0; // Comparison to store the index // from second to second last // index of array 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; } // Comparison to store the index // of last element of array if (arr[n - 1] < arr[n - 2]) min[k1++] = n - 1; else max[k2++] = n - 1; // Count of number of maximal contiguous // increasing and decreasing subsequences 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); } // Driver code static public void Main() { int [] arr = { 12, 8, 11, 13, 10, 15, 14, 16, 20 }; int n = arr.Length; numOfSubseq(arr, n); } } // This code is contributed by ajit. |
Javascript
<script> // Javascript implementation of the above approach. // Function to count the number of maximal contiguous // increasing and decreasing subsequences 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); // k2, k1 are used to store the // count of max and min array let k1 = 0, k2 = 0; // Comparison to store the index of // first element of array if (arr[0] < arr[1]) min[k1++] = 0; else max[k2++] = 0; // Comparison to store the index // from second to second last // index of array 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; } // Comparison to store the index // of last element of array if (arr[n - 1] < arr[n - 2]) min[k1++] = n - 1; else max[k2++] = n - 1; // Count of number of maximal contiguous // increasing and decreasing subsequences 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); // This code is contributed by suresh07. </script> |
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.