g0001_0100.s0001_two_sum.complexity.md Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java Show documentation
Show all versions of leetcode-in-java Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
The newest version!
**Time Complexity (Big O Time):**
The time complexity of this program is O(n), where "n" represents the number of elements in the `numbers` array. Here's the breakdown:
1. The program iterates through the `numbers` array once using a `for` loop, which runs for "n" iterations (n is the length of the array).
2. Inside the loop, it performs constant-time operations:
- It calculates the `requiredNum` (constant time).
- It checks if `requiredNum` is present in the `indexMap` (constant time).
- It puts `numbers[i]` and `i` into the `indexMap` (constant time).
Since all the operations inside the loop are constant time, and the loop itself runs for "n" iterations, the overall time complexity is O(n).
**Space Complexity (Big O Space):**
The space complexity of this program is also O(n), where "n" represents the number of elements in the `numbers` array. Here's why:
1. The program uses a `Map` called `indexMap` to store the numbers and their corresponding indices. In the worst case, it could store all "n" elements from the `numbers` array.
2. The program returns an integer array of size 2 (constant space), regardless of the input size.
The dominant factor in terms of space complexity is the `indexMap`, which can potentially store "n" key-value pairs. Therefore, the space complexity is O(n).
© 2015 - 2024 Weber Informatics LLC | Privacy Policy