#2 modified algo approach