Below is the link of solution. Ask your doubts in comments below. For more clarity, refer the solution code. If you understand the solution, Like the video and Subscribe to my channel to get more updates
Time and space complexity is included in the solution.
https://onecompiler.com/cpp/43dyy938n