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 .
int [ ] newvec = new int [ m + 1 ] ; for ( int i = 0 ; i < m + 1 ; i ++ ) freqarr [ m - 1 ] . add ( newvec [ i ] ) ; for ( int i = m - 2 ; i > 0 ; i -- ) { Vector < Integer > nv = new Vector < > ( ) ; nv = freqarr [ i + 1 ] ; nv . insertElementAt ( arr [ i + 1 ] + 1 , arr [ i + 1 ] ) ; freqarr [ i ] = nv ; }