Tape Equillibrium(Codility) impl in java