update algorithm without using Arrays.copyOfRange