SRM 521 Div2 Linear Time Solution