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

g0101_0200.s0189_rotate_array.complexity.md Maven / Gradle / Ivy

There is a newer version: 1.35
Show newest version
**Time Complexity (Big O Time):**

The time complexity of this program is O(n), where n is the length of the input array `nums`. Here's why:

1. The `reverse` method has a while loop that iterates from `l` to `r`, where `l` and `r` are indices within the array. The number of iterations in this loop is proportional to the size of the subarray being reversed.

2. In the `rotate` method, there are three calls to the `reverse` method:
   - The first `reverse` call reverses the subarray from index 0 to `t - 1`, where `t` is calculated as `n - (k % n)`.
   - The second `reverse` call reverses the subarray from index `t` to `n - 1`.
   - The third `reverse` call reverses the entire array from index 0 to `n - 1`.

All three `reverse` calls have time complexity proportional to the size of the subarray being reversed, and they operate on non-overlapping subarrays. Therefore, the overall time complexity is O(n).

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

The space complexity of this program is O(1), indicating that it uses a constant amount of additional memory regardless of the size of the input array. The program performs the rotation in-place, and the space used for variables and computations remains constant.

In summary, the time complexity is O(n), where n is the length of the input array, and the space complexity is O(1), indicating constant space usage.




© 2015 - 2024 Weber Informatics LLC | Privacy Policy