Time Complexity (Big O Time):
-
The program first iterates backward through the array to find the first element
i
wherenums[i] < nums[i+1]
. This operation takes O(n) time, where n is the length of the array. -
If such an element
i
is found, the program then iterates backward again to find the smallest elementj
to the right ofi
such thatnums[j] > nums[i]
. This operation also takes O(n) time, where n is the length of the array. -
After finding
i
andj
, the program swaps these two elements, which takes constant time O(1). -
Finally, the program reverses the subarray to the right of
i
(from indexi+1
to the end of the array), which takes O(n) time in the worst case.
Overall, the time complexity of the program is dominated by the two linear scans through the array, so it's O(n), where n is the length of the input array nums
.
Space Complexity (Big O Space):
The space complexity of the program is O(1) because it uses a constant amount of additional space for variables like i
, j
, and temp
. Regardless of the size of the input array, the space used by these variables remains constant.
In summary, the time complexity of the provided program is O(n), and the space complexity is O(1), where n is the length of the input array nums
.