// 演算法 / Algorithm:
// 對每個數字用「% 10 取個位、/ 10 去個位」反覆算出數字和，
// 同時用 ans 追蹤所有數字和的最小值，一次遍歷即可。
// For each number, get its digit sum via repeated %10 and /10,
// while keeping ans as the running minimum over a single pass.

#include <limits.h>   // 為了使用 INT_MAX / for INT_MAX

int minElement(int* nums, int numsSize) {
    int ans = INT_MAX;                 // ans 設為最大值，之後只會變小 / start huge so any real sum is smaller

    for (int i = 0; i < numsSize; i++) {   // 遍歷陣列每個元素 / visit every element by index
        int n = nums[i];               // 取出目前的數字，n 是工作副本 / working copy we can shrink
        int sum = 0;                   // 累加這個數字的各位 / accumulator for this number's digit sum

        while (n > 0) {                // 還有位數沒處理就繼續 / keep going while digits remain
            sum += n % 10;             // n % 10 是個位數，加進 sum / add the last digit
            n /= 10;                   // 整數除法砍掉個位 / drop the last digit (integer division)
        }

        if (sum < ans) {               // 找到更小的數字和就更新 / found a smaller sum -> record it
            ans = sum;                 // 更新最小值 / update the minimum
        }
    }

    return ans;                        // 回傳最小數字和 / return the smallest digit sum
}
