This is a problem where we need to use the knowledge of 2Sum and 2Sum II. We are required to find triplets whose sum equals to 0. We can’t have duplicate triplets and use the same element multiple times.
Code:
Java
Big O Analysis
Runtime
The runtime complexity here is O(N2) since we have one for loop and a while loop nested in it.
Memory
The memory usage is O(1) since we are not using any extra datastructure.