Find the minimum number of elements that should be removed to make an array good | Function to remove minimum elements to make the given array good ; To store count of each subsequence ; Increase the count of subsequence [ 0 ] ; If Previous element subsequence count is greater than zero then increment subsequence count of current element and decrement subsequence count of the previous element . ; Return the required answer ; Driver code ; Function call
function MinRemove ( a , n , k ) { let cnt = new Array ( k ) . fill ( 0 ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( a [ i ] == 0 ) cnt [ 0 ] ++ ; else if ( cnt [ a [ i ] - 1 ] > 0 ) { cnt [ a [ i ] - 1 ] -- ; cnt [ a [ i ] ] ++ ; } } return n - ( k * cnt [ k - 1 ] ) ; } let a = [ 0 , 1 , 2 , 3 , 4 , 0 , 1 , 0 , 1 , 2 , 3 , 4 ] , k = 5 ; let n = a . length ; document . write ( MinRemove ( a , n , k ) ) ;