Nested Loops: A Concern
The given code uses nested loops, resulting in a time complexity of , where is the length of the array. In the worst case, this could lead to sluggish performance, particularly for larger datasets. In a real-world scenario, efficiency often matters a lot, so we'd prefer to optimize the code.

A More Efficient Approach
Can we achieve the same result with just one loop, making it an solution? Yes, we can! Let's break down how we can do this.