Smallest occurring element in each subsequence | C ++ program for the above approach ; Function that count the subsequence such that each element as the minimum element in the subsequence ; Store index in a map ; Sort the array ; To store count of subsequence ; Traverse the array ; Store count of subsequence ; Print the count of subsequence ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE 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 ) ; }