Plus One [C++]
IntuitionThe problem requires adding 1 to a number represented as an array of digits. Since addition starts from the least significant digit (the rightmost), we
# Intuition
The problem requires adding 1 to a number represented as an array of digits. Since addition starts from the least significant digit (the rightmost), we can iterate the array from the end to handle potential carryovers.

# Approach
1. Iterate through the digits array in reverse order:
   - If the current digit is less than 9, increment it and return the array.
   - If the digit is 9, set it to 0 (carryover) and continue to the next iteration.
2. If the loop completes and all digits were 9 (e.g., `999`), insert `1` at the beginning of the array to account for the carryover, resulting in `1000`.
3. Return the updated array.

# Complexity
- **Time complexity:**  
  $$O(n)$$, where \( n \) is the number of digits. Each digit is processed once in the worst case.
  
- **Space complexity:**  
  $$O(1)$$ extra space (in-place modification), excluding the output array.

# Code
```cpp
class Solution {
public:
    vector<int> plusOne(vector<int>& digits) {
        int n = digits.size();
        for (int i = n - 1; i >= 0; --i) {
            if (digits[i] < 9) {
                digits[i]++;
                return digits;
            }
            digits[i] = 0;
        }
        digits.insert(digits.begin(), 1);
        return digits;
    }
};
```
['C++']