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