Count all increasing subsequences | Function To Count all the sub - sequences possible in which digit is greater than all previous digits arr [ ] is array of n digits ; count [ ] array is used to store all sub - sequences possible using that digit count [ ] array covers all the digit from 0 to 9 ; Scan each digit in arr [ ] ; Count all possible sub - sequences by the digits less than arr [ i ] digit ; Store sum of all sub - sequences plus 1 in count [ ] array ; Now sum up the all sequences possible in count [ ] array ; Driver code
function countSub ( arr , n ) { let count = new Array ( 10 ) . fill ( 0 ) ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = arr [ i ] - 1 ; j >= 0 ; j -- ) count [ arr [ i ] ] += count [ j ] ; count [ arr [ i ] ] ++ ; } let result = 0 ; for ( let i = 0 ; i < 10 ; i ++ ) result += count [ i ] ; return result ; } let arr = [ 3 , 2 , 4 , 5 , 4 ] ; let n = arr . length ; document . write ( countSub ( arr , n ) ) ;