Related Articles
Smallest occurring element in each subsequence
• Difficulty Level : Easy
• Last Updated : 27 Aug, 2020

Given an array of N distinct integers. For each lement the task is to find the count of subsequence from all the possible subsequence whose minimum element is the current element.
Examples:

Input: arr[] = {1, 2}
Output: 2 1
Explanation:
Subsequences are {1}, {2}, {1, 2}.
The count of the smallest element in each subsequence is:
1 = 2, 2 = 1
Input: arr[] = {1, 2, 3}
Output: 4 2 1

Naive Approach: The idea is to generate all possible subsequences of the given array and count the smallest element in each subsequence and print its count for each element in the array.
Time Complexity: O(2N
Auxiliary Space: O(N)

Efficient Approach: The idea is to observe a pattern i.e., so observe that minimum element occurs 2n – 1 times, the second minimum occurs 2n – 2 times and so on …. For Example:

Let the array be arr[] = {1, 2, 3}
Subsequences are {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}
Minimum of each subsequence: {1}, {2}, {3}, {1}, {1}, {2}, {1}.
where
1 occurs 4 times i.e. 2n – 1 where n = 3.
2 occurs 2 times i.e. 2n – 2 where n = 3.
3 occurs 1 times i.e. 2n – 3 where n = 3.

Below are the steps:

1. Store the index of each element in a Map such that we can print the element in the order of the original array.
2. Sort the given array.
3. Now the elements are in increasing order and from the above observation traverse the given array and keep the count of subsequence such that each element is the smallest element is given by pow(2, N – 1 – i).
4. Now traverse the map and print count of subsequence according to the element in the original array.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach` `#include ` `using` `namespace` `std;`   `// Function that count the subsequence` `// such that each element as the` `// minimum element in the subsequence` `void` `solve(``int` `arr[], ``int` `N)` `{` `    ``map<``int``, ``int``> M;`   `    ``// Store index in a map` `    ``for` `(``int` `i = 0; i < N; i++) {` `        ``M[i] = arr[i];` `    ``}`   `    ``// Sort the array` `    ``sort(arr, arr + N);`   `    ``// To store count of subsequence` `    ``unordered_map<``int``, ``int``> Count;`   `    ``// Traverse the array` `    ``for` `(``int` `i = 0; i < N; i++) {`   `        ``// Store count of subsequence` `        ``Count[arr[i]] = ``pow``(2, N - i - 1);` `    ``}`   `    ``// Print the count of subsequence` `    ``for` `(``auto``& it : M) {` `        ``cout << Count[M[it.second]] << ``' '``;` `    ``}` `}`   `// Driver code` `int` `main()` `{` `    ``int` `arr[] = { 5, 2, 1 };` `    ``int` `N = ``sizeof` `arr / ``sizeof` `arr[0];`   `    ``// Function call` `    ``solve(arr, N);` `}`

## Java

 `// Java program for the above approach` `import` `java.util.*;` `class` `GFG{`   `// Function that count the subsequence` `// such that each element as the` `// minimum element in the subsequence` `static` `void` `solve(``int` `arr[], ``int` `N)` `{` `    ``HashMap M = ``new` `HashMap<>();`   `    ``// Store index in a map` `    ``for` `(``int` `i = ``0``; i < N; i++)` `    ``{` `        ``M.put(i, arr[i]);` `    ``}`   `    ``// Sort the array` `    ``Arrays.sort(arr);`   `    ``// To store count of subsequence` `    ``HashMap Count = ``new` `HashMap<>();`   `    ``// Traverse the array` `    ``for` `(``int` `i = ``0``; i < N; i++) ` `    ``{`   `        ``// Store count of subsequence` `        ``Count.put(arr[i], ` `                 ``(``int``)Math.pow(``2``, N - i - ``1``));` `    ``}`   `    ``// Print the count of subsequence` `    ``for` `(Map.Entry m : M.entrySet())` `    ``{` `        ``System.out.print(Count.get(m.getValue()) + ``" "``);` `    ``}` `}`   `// Driver code` `public` `static` `void` `main(String[] args)` `{` `    ``int` `arr[] = { ``5``, ``2``, ``1` `};` `    ``int` `N = arr.length;`   `    ``// Function call` `    ``solve(arr, N);` `}` `}`   `// This code is contributed by Amit Katiyar`

## Python3

 `# Python3 program for the above approach`   `# Function that count the subsequence` `# such that each element as the` `# minimum element in the subsequence` `def` `solve(arr, N):`   `    ``M ``=` `{}`   `    ``# Store index in a map` `    ``for` `i ``in` `range``(N):` `        ``M[i] ``=` `arr[i]` `    `  `    ``# Sort the array` `    ``arr.sort()`   `    ``# To store count of subsequence` `    ``Count ``=` `{}`   `    ``# Traverse the array` `    ``for` `i ``in` `range``(N):`   `        ``# Store count of subsequence` `        ``Count[arr[i]] ``=` `pow``(``2``, N ``-` `i ``-` `1``)`   `    ``# Print the count of subsequence` `    ``for` `it ``in` `Count.values():` `        ``print``(it, end ``=` `" "``)`   `# Driver code` `if` `__name__ ``=``=` `"__main__"``:`   `    ``arr ``=` `[ ``5``, ``2``, ``1` `]` `    ``N ``=` `len``(arr)`   `    ``# Function call` `    ``solve(arr, N)`   `# This code is contributed by chitranayal`

## C#

 `// C# program for the above approach` `using` `System;` `using` `System.Collections.Generic;`   `class` `GFG{`   `// Function that count the subsequence` `// such that each element as the` `// minimum element in the subsequence` `static` `void` `solve(``int` `[]arr, ``int` `N)` `{` `    ``Dictionary<``int``,` `               ``int``> M = ``new` `Dictionary<``int``,` `                                         ``int``>();`   `    ``// Store index in a map` `    ``for` `(``int` `i = 0; i < N; i++)` `    ``{` `        ``M.Add(i, arr[i]);` `    ``}`   `    ``// Sort the array` `    ``Array.Sort(arr);`   `    ``// To store count of subsequence` `    ``Dictionary<``int``,` `               ``int``> Count = ``new` `Dictionary<``int``,` `                                             ``int``>();`   `    ``// Traverse the array` `    ``for` `(``int` `i = 0; i < N; i++) ` `    ``{`   `        ``// Store count of subsequence` `        ``Count.Add(arr[i], ` `                 ``(``int``)Math.Pow(2, N - i - 1));` `    ``}` `  ``// Print the count of subsequence` `   ``foreach``(KeyValuePair<``int``, ``int``> m ``in` `M)` `{` `    ``Console.Write(Count[m.Value]);   ` `}  ` `}`   `// Driver code` `public` `static` `void` `Main(String[] args)` `{` `    ``int` `[]arr = { 5, 2, 1 };` `    ``int` `N = arr.Length;`   `    ``// Function call` `    ``solve(arr, N);` `}` `}`   `// This code is contributed by PrinciRaj1992`

Output:

```4 2 1

```

Time Complexity: O(N*log N)
Auxiliary Space: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :