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++
#include <bits/stdc++.h>
using namespace std;
void solve( int arr[], int N)
{
map< int , int > M;
for ( int i = 0; i < N; i++) {
M[i] = arr[i];
}
sort(arr, arr + N);
unordered_map< int , int > Count;
for ( int i = 0; i < N; i++) {
Count[arr[i]] = pow (2, N - i - 1);
}
for ( auto & it : M) {
cout << Count[M[it.second]] << ' ' ;
}
}
int main()
{
int arr[] = { 5, 2, 1 };
int N = sizeof arr / sizeof arr[0];
solve(arr, N);
}
|
Java
import java.util.*;
class GFG{
static void solve( int arr[], int N)
{
HashMap<Integer,
Integer> M = new HashMap<>();
for ( int i = 0 ; i < N; i++)
{
M.put(i, arr[i]);
}
Arrays.sort(arr);
HashMap<Integer,
Integer> Count = new HashMap<>();
for ( int i = 0 ; i < N; i++)
{
Count.put(arr[i],
( int )Math.pow( 2 , N - i - 1 ));
}
for (Map.Entry<Integer,
Integer> m : M.entrySet())
{
System.out.print(Count.get(m.getValue()) + " " );
}
}
public static void main(String[] args)
{
int arr[] = { 5 , 2 , 1 };
int N = arr.length;
solve(arr, N);
}
}
|
Python3
def solve(arr, N):
M = {}
for i in range (N):
M[i] = arr[i]
arr.sort()
Count = {}
for i in range (N):
Count[arr[i]] = pow ( 2 , N - i - 1 )
for it in Count.values():
print (it, end = " " )
if __name__ = = "__main__" :
arr = [ 5 , 2 , 1 ]
N = len (arr)
solve(arr, N)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void solve( int []arr, int N)
{
Dictionary< int ,
int > M = new Dictionary< int ,
int >();
for ( int i = 0; i < N; i++)
{
M.Add(i, arr[i]);
}
Array.Sort(arr);
Dictionary< int ,
int > Count = new Dictionary< int ,
int >();
for ( int i = 0; i < N; i++)
{
Count.Add(arr[i],
( int )Math.Pow(2, N - i - 1));
}
foreach (KeyValuePair< int , int > m in M)
{
Console.Write(Count[m.Value]);
}
}
public static void Main(String[] args)
{
int []arr = { 5, 2, 1 };
int N = arr.Length;
solve(arr, N);
}
}
|
Javascript
<script>
function solve(arr, N)
{
var M = new Map();
for ( var i = 0; i < N; i++) {
M.set(i, arr[i]);
}
arr.sort((a,b)=>a-b);
var Count = new Map();
for ( var i = 0; i < N; i++) {
Count.set(arr[i], parseInt(Math.pow(2, N - i - 1)));
}
M.forEach((value, key) => {
document.write(Count.get(value) + ' ' );
});
}
var arr = [5, 2, 1];
var N = arr.length;
solve(arr, N);
</script>
|
Time Complexity: O(N*log N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...