<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 給定范圍內具有相等元素的索引數 > 原文: [https://www.geeksforgeeks.org/number-indexes-equal-elements-given-range/](https://www.geeksforgeeks.org/number-indexes-equal-elements-given-range/) 給定`N`個數字和`Q`個查詢,每個查詢都由`L`和`R`組成,任務是找到這樣的整數`i`(`L <= i < R`)的數量,以使`A[i] = A[i+1]`。 考慮基于 0 的索引。 **示例**: ``` Input : A = [1, 2, 2, 2, 3, 3, 4, 4, 4] Q = 2 L = 1 R = 8 L = 0 R = 4 Output : 5 2 Explanation: We have 5 index i which has Ai=Ai+1 in range [1, 8). We have 2 indexes i which have Ai=Ai+1 in range [0, 4). Input :A = [3, 3, 4, 4] Q = 2 L = 0 R = 3 L = 2 R = 3 Output : 2 1 ``` **樸素的方法**是從`L`遍歷到`R`(不包括`R`)并分別針對每個查詢,計算滿足條件`A[i] = A[i+1]`的索引`i`的數量。 以下是樸素方法的實現: ## C++ ```cpp // CPP program to count the number of indexes // in range L R such that Ai = Ai+1 #include <bits/stdc++.h> using namespace std; // function that answers every query in O(r-l) int answer_query(int a[], int n, int l, int r) { ????// traverse from l to r and count ????// the required indexes ????int count = 0; ????for (int i = l; i < r; i++) ????????if (a[i] == a[i + 1]) ????????????count += 1; ????return count; } // Driver Code int main() { ????int a[] = { 1, 2, 2, 2, 3, 3, 4, 4, 4 }; ????int n = sizeof(a) / sizeof(a[0]); ????// 1-st query ????int L, R; ????L = 1; ????R = 8; ????cout << answer_query(a, n, L, R) << endl; ????// 2nd query ????L = 0; ????R = 4; ????cout << answer_query(a, n, L, R) << endl; ????return 0; } ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看