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

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

The newest version!
**Time Complexity (Big O Time):**

1. Sorting: The program sorts the input array `nums`. Sorting typically takes O(n * log(n)) time complexity, where 'n' is the length of the array.

2. Main Loop: The program uses nested loops. The outer loop runs from 0 to `len - 2`, where `len` is the length of the sorted array `nums`. The inner while loop has two pointers (`l` and `r`) that move towards each other. In the worst case, the inner loop can go through the entire array, so its time complexity is O(n).

3. The code inside the inner loop contains constant-time operations, such as addition, comparison, and list creation.

Overall, the dominant time complexity is determined by the sorting step, which is O(n * log(n)). The loop inside the sorting step has a complexity of O(n), but it doesn't dominate the overall complexity.

So, the total time complexity of the program is O(n * log(n)) due to the sorting step.

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

The space complexity of the program is determined by the space used for the output list `result`. In the worst case, when there are many unique triplets that sum to zero, the size of `result` can be significant.

1. The input array `nums` and the variables `len`, `l`, and `r` all use constant space.

2. The `result` list stores the triplets, and its size can be at most O(n^2) when there are many unique triplets.

3. Other variables and temporary lists used inside the loops use constant space.

So, the space complexity of the program is O(n^2) for the `result` list when considering the worst case.

In summary, the provided program has a time complexity of O(n * log(n)) due to sorting and a space complexity of O(n^2) for the `result` list when considering the worst-case scenario.




© 2015 - 2024 Weber Informatics LLC | Privacy Policy