All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0001_0100.s0015_3sum.complexity.md Maven / Gradle / Ivy

**Time Complexity (Big O Time):**

The time complexity of this program is O(n^2), where "n" represents the number of elements in the `nums` array. Here's the breakdown:

1. The program starts by sorting the `nums` array, which has a time complexity of O(n * log(n)), where "n" is the length of the array.

2. After sorting, the program uses nested loops:
   - The outer loop runs for `i` from 0 to `len - 2`, where "len" is the length of the array. This loop iterates through each element of the array once, so it has a time complexity of O(n).

   - The inner loop uses two pointers (`l` and `r`) and runs until `r` is greater than `l`. Within the inner loop, constant-time operations are performed, such as calculating the sum of three elements and adjusting the pointers.

3. Inside the inner loop, there are additional while loops that skip duplicate elements. These while loops also perform constant-time operations.

Since the sorting step has a time complexity of O(n * log(n)), and the nested loops contribute O(n) iterations, the overall time complexity is dominated by the sorting step, resulting in O(n * log(n)).

**Space Complexity (Big O Space):**

The space complexity of this program is O(1) because it uses a constant amount of extra space. The program creates a few integer variables (`l`, `r`, `sum`, and `len`), but the space used by these variables is independent of the input size. Additionally, the `result` list stores the output, but its space is not considered part of the space complexity analysis, as it's required to store the program's output.

Therefore, the overall space complexity is O(1).




© 2015 - 2025 Weber Informatics LLC | Privacy Policy