Smallest occurring element in each subsequence | Java 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
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 ) ; } }