Watch tutorials and explanations by expert teachers that walk through solving this problem.
Jenny's Lectures CS IT
NeetCode
Greg Hogg
NeetCodeIO
GeeksforGeeks
EZCSE
Solve more problems to master this pattern