/*
 * 演算法 / Algorithm:
 * 與 C 版本完全相同的雙指針法，但使用 STL 的 vector 容器。
 * Same two-pointer algorithm as the C version, but using STL's vector.
 * Time O(n), Space O(1) extra (output vector not counted).
 */

#include <vector>  // 為了 std::vector / for std::vector

class Solution {
public:
    // vector<int> 是 C++ 標準函式庫提供的動態陣列（可變長度的陣列）
    // vector<int> is C++'s standard dynamic array (resizable array).
    // 參數加 const& 表示「不會修改且不複製」，避免大陣列複製成本
    // const& means "won't modify, won't copy" — avoids copying the input vector.
    std::vector<int> twoSum(const std::vector<int>& numbers, int target) {
        // .size() 回傳元素個數，型別是 size_t（無號整數）
        // 轉成 int 以便和指針做減法不會有警告
        // .size() returns the count as size_t (unsigned); cast to int for safe arithmetic.
        int left = 0;
        int right = static_cast<int>(numbers.size()) - 1;

        // 主迴圈：兩指針未交錯就繼續 / Main loop: while pointers haven't crossed
        while (left < right) {
            // numbers[left] 用 operator[] 讀取元素，和 C 陣列語法一樣
            // numbers[left] uses operator[] — same indexing syntax as a C array.
            int sum = numbers[left] + numbers[right];

            if (sum == target) {
                // 用列表初始化 (brace-init) 直接建構並回傳 vector
                // 1-indexed 所以兩個都 + 1
                // Brace-initialize and return the vector directly; +1 for 1-indexing.
                return {left + 1, right + 1};
            } else if (sum < target) {
                // ++left 是前置遞增，等同於 left = left + 1
                // ++left is pre-increment, equivalent to left = left + 1.
                ++left;
            } else {
                // --right 把右指針往左移一格 / move right pointer one step left
                --right;
            }
        }

        // 題目保證有解，回傳空 vector 作為安全 fallback
        // Problem guarantees a solution; empty vector as an unreachable fallback.
        return {};
    }
};
