// 演算法：把陣列當成首尾相連的環，數有幾個位置 nums[i] > nums[(i+1) % n]。
// Algorithm: treat the array as a circle and count positions where
// nums[i] > nums[(i+1) % n]. A sorted-then-rotated array has at most one such
// "drop" (the seam). If we see two or more drops, it cannot be sorted+rotated.

#include <stdbool.h>   // 引入 bool / pull in the bool type

// LeetCode 的 C 介面：nums 是輸入陣列指標，numsSize 是長度。
// LeetCode C signature: nums is a pointer to the input array, numsSize its length.
bool check(int* nums, int numsSize) {
    int drops = 0;                              // 計算下降次數 / count of descents
    for (int i = 0; i < numsSize; i++) {        // 走訪每一個索引 i / iterate every index i
        // (i + 1) % numsSize 把 i = numsSize-1 的「下一個」繞回到 0。
        // (i + 1) % numsSize wraps the "next index" of the last position back to 0,
        // giving us the circular neighbor.
        int next = (i + 1) % numsSize;          // 下一個索引（環狀）/ next index (circular)
        if (nums[i] > nums[next]) {             // 出現下降 / a descent occurred
            drops++;                            // 累加下降次數 / increment the counter
            if (drops > 1) return false;        // 超過 1 次直接失敗，提早結束 / >1 → fail fast
        }
    }
    // drops 為 0（完全沒旋轉）或 1（有一個接縫）都合法。
    // drops == 0 (no rotation) or drops == 1 (one seam) are both valid.
    return true;
}
