IBM Interview Question

How can you optimize the above codes?