Count of possible subarrays and subsequences using given length of Array Last Updated : 18 Nov, 2021 Summarize Suggest changes Share Like Article Like Report 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++ // 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 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 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)) 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 JavaScript <script> // JavaScript implementation to count // the subarray and subsequence of // given length of the array // Function to count the subarray // for the given array function countSubarray(n){ return ((n)*(n + 1))/2; } // Function to count the subsequence // for the given array length function countSubsequence(n){ return Math.pow(2, n); } // Driver Code let n = 5; document.write((countSubarray(n)) +"<br/>"); document.write((countSubsequence(n)) +"\n"); </script> Output: 15 32 Time Complexity: O(log n) Auxiliary Space: O(1) Advertise with us Next Article Count of Subsequences of given string X in between strings Y and Z _mridul_bhardwaj_ Follow Similar Reads Subarrays, Subsequences, and Subsets in Array What is a Subarray?A subarray is a contiguous part of array, i.e., Subarray is an array that is inside another array. In general, for an array of size n, there are n*(n+1)/2 non-empty subarrays. For example, Consider the array [1, 2, 3, 4], There are 10 non-empty sub-arrays. The subarrays are: (1), 10 min read Count of unique Subsequences of given String with lengths in range [0, N] Given a string S of length N, the task is to find the number of unique subsequences of the string for each length from 0 to N. Note: The uppercase letters and lowercase letters are considered different and the result may be large so print it modulo 1000000007. Examples: Input: S = "ababd"Output: Num 14 min read Count of Subsequences of given string X in between strings Y and Z Given three strings, 'X', 'Y' and 'Z', the task is to count the number of subsequences of 'X' which is lexicographically greater than or equal to 'Y' and lexicographically lesser than or equal to 'Z'. Examples: Input: X = "abc", Y = "a", Z = "bc"Output: 6Explanation: The subsequences of X which are 15+ min read Count of subsequence of an Array having all unique digits Given an array A containing N positive integers, the task is to find the number of subsequences of this array such that in each subsequence , no digit is repeated twice, i.e. all the digits of the subsequences must be unique.Examples: Input: A = [1, 12, 23, 34] Output: 7 The subsequences are: {1}, { 15+ min read Count of subsequences having odd Bitwise AND values in the given array Given an array arr[] of N integers, the task is to find the number of subsequences of the given array such that their Bitwise AND value is Odd. Examples: Input: arr[] = {2, 3, 1}Output: 3Explanation: The subsequences of the given array having odd Bitwise AND values are {3} = 3, {1} = 1, {3, 1} = 3 5 min read Generating all possible Subsequences using Recursion including the empty one. Given an array arr[]. The task is to find all the possible subsequences of the given array using recursion.Examples: Input: arr[] = [1, 2, 3]Output : [3], [2], [2, 3], [1], [1, 3], [1, 2], [1, 2, 3], []Input: arr[] = [1, 2]Output : [2], [1], [1, 2], []Approach:For every element in the array, there a 5 min read Article Tags : DSA subsequence subarray Like