My algorithm exercise