life is too short for a diary

Tags: leetcode two pointers java

Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.^{1}

This solution will result in **Time Limit Exceeded** since it has complexity of **O(n ^{3})**. We can naively check for all combination of array in three nested loops.

Using two pointer solution, we can acheive **O(n ^{2})** time complexity.

comments powered by Disqus