Given an integer **N** which denotes the length of an array, the task is to count the number of subarray and subsequence possible with the given length of the array.

**Examples:**

Input:N = 5

Output:

Count of subarray = 15

Count of subsequence = 32

Input:N = 3

Output:

Count of subarray = 6

Count of subsequence = 8

**Approach:** The key observation fact for the count of the subarray is the number of ends position possible for each index elements of the array can be (N – i), Therefore the count of the subarray for an array of size **N** can be:

Count of Sub-arrays = (N) * (N + 1) --------------- 2

The key observation fact for the count of the subsequence possible is each element of the array can be included in a subsequence or not. Therefore, the choice for each element is 2.

Count of subsequences = 2^{N}

Below is the implementation of the above approach:

## C++

`// C++ implementation to count ` `// the subarray and subsequence of ` `// given length of the array ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to count the subarray ` `// for the given array ` `int` `countSubarray(` `int` `n){ ` ` ` `return` `((n)*(n + 1))/2; ` `} ` ` ` `// Function to count the subsequence ` `// for the given array length ` `int` `countSubsequence(` `int` `n){ ` ` ` `return` `pow` `(2, n); ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `n = 5; ` ` ` `cout << (countSubarray(n)) << endl; ` ` ` `cout << (countSubsequence(n)) << endl; ` ` ` `return` `0; ` `} ` ` ` `// This code is contributed by mohit kumar 29 ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation to count ` `// the subarray and subsequence of ` `// given length of the array ` `class` `GFG{ ` ` ` `// Function to count the subarray ` `// for the given array ` `static` `int` `countSubarray(` `int` `n){ ` ` ` `return` `((n)*(n + ` `1` `))/` `2` `; ` `} ` ` ` `// Function to count the subsequence ` `// for the given array length ` `static` `int` `countSubsequence(` `int` `n){ ` ` ` `return` `(` `int` `) Math.pow(` `2` `, n); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `n = ` `5` `; ` ` ` `System.out.print((countSubarray(n)) +` `"\n"` `); ` ` ` `System.out.print((countSubsequence(n)) +` `"\n"` `); ` `} ` `} ` ` ` `// This code is contributed by Princi Singh ` |

*chevron_right*

*filter_none*

## Python

`# Python implementation to count ` `# the subarray and subsequence of ` `# given length of the array ` ` ` `# Function to count the subarray ` `# for the given array ` `def` `countSubarray(n): ` ` ` `return` `((n)` `*` `(n ` `+` `1` `))` `/` `/` `2` ` ` `# Function to count the subsequence ` `# for the given array length ` `def` `countSubsequence(n): ` ` ` `return` `(` `2` `*` `*` `n) ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` `n ` `=` `5` ` ` `print` `(countSubarray(n)) ` ` ` `print` `(countSubsequence(n)) ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation to count ` `// the subarray and subsequence of ` `// given length of the array ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function to count the subarray ` `// for the given array ` `static` `int` `countSubarray(` `int` `n){ ` ` ` `return` `((n)*(n + 1))/2; ` `} ` ` ` `// Function to count the subsequence ` `// for the given array length ` `static` `int` `countSubsequence(` `int` `n){ ` ` ` `return` `(` `int` `) Math.Pow(2, n); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `n = 5; ` ` ` `Console.Write((countSubarray(n)) +` `"\n"` `); ` ` ` `Console.Write((countSubsequence(n)) +` `"\n"` `); ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

**Output:**

15 32

## Recommended Posts:

- Find product of all elements at indexes which are factors of M for all possible sorted subsequences of length M
- Count the number of subsequences of length k having equal LCM and HCF
- Count unique subsequences of length K
- Count of subsequences of length 4 in form (x, x, x+1, x+1) | Set 2
- Count of subsequences of length atmost K containing distinct prime elements
- Generating all possible Subsequences using Recursion
- Maximum length L such that the sum of all subarrays of length L is less than K
- Unique subsequences of length K with given sum
- Maximize subsequences having array elements not exceeding length of the subsequence
- Count of Subarrays in an array containing numbers from 1 to the length of subarray
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Differences between number of increasing subarrays and decreasing subarrays in k sized windows
- Maximize count of Decreasing Subsequences from the given Array
- Count of subsequences from a given Array having Binary Equivalence
- Queries to find maximum sum contiguous subarrays of given length in a rotating array
- Count of contiguous subarrays possible for every index by including the element at that index
- Count of longest possible subarrays with sum not divisible by K
- Count of all possible bitonic subarrays
- Count of all possible reverse bitonic subarrays
- Count subsequences which contains both the maximum and minimum array element

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.