Fill up the freqarr vector from last to first . After m - 1 th position frequency of all type of tasks will be 0. Then at m - 2 th position only frequency of arr [ m - 1 ] type task will be increased by 1. Again , in m - 3 th position only frequency of type arr [ m - 2 ] task will be increased by 1 and so on .
newvec = [ 0 ] * ( m + 1 ) NEW_LINE freqarr [ m - 1 ] = newvec [ : ] NEW_LINE for i in range ( m - 2 , - 1 , - 1 ) : NEW_LINE INDENT nv = freqarr [ i + 1 ] [ : ] NEW_LINE nv [ arr [ i + 1 ] ] += 1 NEW_LINE freqarr [ i ] = nv [ : ] NEW_LINE DEDENT