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

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

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

The time complexity of this program is O(N), where N is the length of the `arr` input array. The program makes two passes over the array:

1. **Forward Pass:** In the first pass, it iterates through the array from left to right, calculating the cumulative product of elements in the `cprod` variable. It also keeps track of the maximum product seen so far in the `ans` variable. This pass takes O(N) time.

2. **Backward Pass:** In the second pass, it iterates through the array from right to left, again calculating the cumulative product in `cprod` and updating the maximum product in `ans`. This pass also takes O(N) time.

Both passes run in linear time, and there are no nested loops or recursive calls, so the overall time complexity is O(N).

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

The space complexity of this program is O(1), meaning it uses a constant amount of additional space that does not depend on the size of the input array `arr`. The program uses a fixed number of variables (`ans`, `cprod`, `i`, `j`) and doesn't create any data structures or data collections with a size proportional to `N`.

In summary, this program has a time complexity of O(N) and a space complexity of O(1).




© 2015 - 2024 Weber Informatics LLC | Privacy Policy