solution with single array. still very slow.