Changing from for loop to functional style

can somebody please help me out with this problem:


Thanks.

Is there anything wrong with the answer you’ve got on SO?

thanks guys for taking time and suggesting solutions.