Count subsequences which contains both the maximum and minimum array element | Java program for the above approach ; Function to calculate the count of subsequences ; Find the maximum from the array ; Find the minimum from the array ; If array contains only one distinct element ; Find the count of maximum ; Find the count of minimum ; Finding the result with given condition ; Driver Code ; Function call
import java . util . Arrays ; class GFG { static double countSubSequence ( int [ ] arr , int n ) { int maximum = Arrays . stream ( arr ) . max ( ) . getAsInt ( ) ; int minimum = Arrays . stream ( arr ) . min ( ) . getAsInt ( ) ; if ( maximum == minimum ) return Math . pow ( 2 , n ) - 1 ; int i = count ( arr , maximum ) ; int j = count ( arr , minimum ) ; double res = ( Math . pow ( 2 , i ) - 1 ) * ( Math . pow ( 2 , j ) - 1 ) * Math . pow ( 2 , n - i - j ) ; return res ; } static int count ( int [ ] arr , int value ) { int sum = 0 ; for ( int i = 0 ; i < arr . length ; i ++ ) if ( arr [ i ] == value ) sum ++ ; return sum ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int n = arr . length ; System . out . println ( countSubSequence ( arr , n ) ) ; } }