Count subsequences which contains both the maximum and minimum array element | 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
function countSubSequence ( arr , n ) { let maximum = Math . max ( ... arr ) ; let minimum = Math . min ( ... arr ) ; if ( maximum == minimum ) return Math . pow ( 2 , n ) - 1 ; let i = count ( arr , maximum ) ; let j = count ( arr , minimum ) ; let res = ( Math . pow ( 2 , i ) - 1 ) * ( Math . pow ( 2 , j ) - 1 ) * Math . pow ( 2 , n - i - j ) ; return res ; } function count ( arr , value ) { let sum = 0 ; for ( let i = 0 ; i < arr . length ; i ++ ) if ( arr [ i ] == value ) sum ++ ; return sum ; } let arr = [ 1 , 2 , 3 , 4 ] ; let n = arr . length ; document . write ( countSubSequence ( arr , n ) ) ;