Count of possible subarrays and subsequences using given length of Array

18 Nov, 2021
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 = 32Input: 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 = 2N
Below is the implementation of the above approach:  
C++
Java
Python
C#
Javascript
// 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 arrayint countSubarray(int n){    return ((n)*(n + 1))/2;} // Function to count the subsequence// for the given array lengthint countSubsequence(int n){    return pow(2, n);} // Driver Codeint main(){    int n = 5;    cout << (countSubarray(n)) << endl;    cout << (countSubsequence(n)) << endl;    return 0;} // This code is contributed by mohit kumar 29
// Java implementation to count// the subarray and subsequence of// given length of the arrayclass GFG{  // Function to count the subarray// for the given arraystatic int countSubarray(int n){    return ((n)*(n + 1))/2;}  // Function to count the subsequence// for the given array lengthstatic int countSubsequence(int n){    return (int) Math.pow(2, n);}  // Driver Codepublic 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
# Python implementation to count# the subarray and subsequence of# given length of the array # Function to count the subarray# for the given arraydef countSubarray(n):    return ((n)*(n + 1))//2     # Function to count the subsequence# for the given array lengthdef countSubsequence(n):    return (2**n) # Driver Code   if __name__ == "__main__":    n = 5    print(countSubarray(n))    print(countSubsequence(n))
// C# implementation to count// the subarray and subsequence of// given length of the arrayusing System; class GFG{   // Function to count the subarray// for the given arraystatic int countSubarray(int n){    return ((n)*(n + 1))/2;}   // Function to count the subsequence// for the given array lengthstatic int countSubsequence(int n){    return (int) Math.Pow(2, n);}   // Driver Codepublic 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
<script> // JavaScript implementation to count// the subarray and subsequence of// given length of the array // Function to count the subarray// for the given arrayfunction countSubarray(n){    return ((n)*(n + 1))/2;}    // Function to count the subsequence// for the given array lengthfunction countSubsequence(n){    return  Math.pow(2, n);} // Driver Code     let n = 5;    document.write((countSubarray(n)) +"<br/>");   document.write((countSubsequence(n)) +"\n"); </script>
15
32
 
Time Complexity: O(log n)
Auxiliary Space: O(1)
mohit kumar 29
princi singh
Rajput-Ji
susmitakundugoaldanga
subham348
subarray
subsequence
Arrays
Competitive Programming
Mathematical
Arrays
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Maximum and minimum of an array using minimum number of comparisons
Top 50 Array Coding Problems for Interviews
Multidimensional Arrays in Java
Stack Data Structure (Introduction and Program)
Linear Search
Competitive Programming - A Complete Guide
Practice for cracking any coding interview
Arrow operator -> in C/C++ with Examples
Modulo 10^9+7 (1000000007)
Prefix Sum Array - Implementation and Applications in Competitive Programming