If you are getting a segmentation fault in large test-cases, (efficiency), it might be because of use of recursion, which needs O(N) stack-space (depending on implementation).
Try converting the recursive solution to iterative one, and get AC.
Regarding segmentation fault in large test-cases (Efficiency part)
ravibitsgoa
#1