Smallest occurring element in each subsequence
Last Updated :
29 Nov, 2021
Given an array of N distinct integers. For each element 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:
- Store the index of each element in a Map such that we can print the element in the order of the original array.
- Sort the given array.
- 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).
- 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 <bits/stdc++.h>
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<Integer,
Integer> 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<Integer,
Integer> 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<Integer,
Integer> 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
JavaScript
<script>
// Javascript program for the above approach
// Function that count the subsequence
// such that each element as the
// minimum element in the subsequence
function solve(arr, N)
{
var M = new Map();
// Store index in a map
for (var i = 0; i < N; i++) {
M.set(i, arr[i]);
}
// Sort the array
arr.sort((a,b)=>a-b);
// To store count of subsequence
var Count = new Map();
// Traverse the array
for (var i = 0; i < N; i++) {
// Store count of subsequence
Count.set(arr[i], parseInt(Math.pow(2, N - i - 1)));
}
// Print the count of subsequence
M.forEach((value, key) => {
document.write(Count.get(value) + ' ');
});
}
// Driver code
var arr = [5, 2, 1];
var N = arr.length;
// Function call
solve(arr, N);
// This code is contributed by rrrtnx.
</script>
Time Complexity: O(N*log N)
Auxiliary Space: O(N)
Similar Reads
Longest Increasing consecutive subsequence Given N elements, write a program that prints the length of the longest increasing consecutive subsequence. Examples: Input : a[] = {3, 10, 3, 11, 4, 5, 6, 7, 8, 12} Output : 6 Explanation: 3, 4, 5, 6, 7, 8 is the longest increasing subsequence whose adjacent element differs by one. Input : a[] = {6
10 min read
Longest Decreasing Subsequence Given an array of N integers, find the length of the longest subsequence of a given sequence such that all elements of the subsequence are sorted in strictly decreasing order. Examples: Input: arr[] = [15, 27, 14, 38, 63, 55, 46, 65, 85] Output: 3 Explanation: The longest decreasing subsequence is {
14 min read
Smallest substring occurring only once in a given string Given a string S consisting of N lowercase alphabets, the task is to find the length of the smallest substring in S whose occurrence is exactly 1. Examples: Input: S = "abaaba"Output: 2Explanation: The smallest substring in the string S, whose occurrence is exactly 1 is "aa" . Length of this substri
6 min read
Smallest subsequence having GCD equal to GCD of given array Given an array arr[] of size N, the task is to find the smallest subsequence of the given array whose GCD of the subsequence is equal to the GCD of the given array. If more than one such subsequence exists, then print anyone of them. Examples: Input: arr[] = {4, 6, 12}Output: 4 6Explanation: Smalles
7 min read
Longest Consecutive Subsequence Given an array of integers, the task is to find the length of the longest subsequence such that elements in the subsequence are consecutive integers, the consecutive numbers can be in any order. Examples: Input: arr[] = [2, 6, 1, 9, 4, 5, 3]Output: 6Explanation: The consecutive numbers here are from
9 min read