algorith updated to directly update the input array