This problem may look simple, but it’s a great way to understand loops, collections, and…