How to write an efficient code for this problem?I have tried writing the code but it is very inefficient as you can see below.All helps are very much appreciated.Thank you This