<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/maximum-number-local-extrema/](https://www.geeksforgeeks.org/maximum-number-local-extrema/) 您將獲得一個包含 n 個元素的數組。 極值是大于其兩個鄰居或小于其兩個鄰居的元素。 您必須計算給定數組中局部極值的數量。 **注意**:第一個和最后一個元素不是極值。 **示例**: ``` Input : a[] = {1, 5, 2, 5} Output : 2 Input : a[] = {1, 2, 3} Output : 0 ``` **方法**:為了計算極值,我們必須檢查一個元素是最大值還是最小值,即該元素是大于其兩個鄰居還是小于兩個鄰居。 為此,只需在數組上進行迭代,然后對每個元素檢查其成為極值的可能性。 **注意**: a [0]和 a [n-1]分別只有一個鄰居,它們既不是最小值也不是最大值。 ## C++ ```cpp // CPP to find number // of extrema #include <bits/stdc++.h> using namespace std; // function to find? // local extremum int extrema(int a[], int n) { ????int count = 0; ????// start loop from position 1 ????// till n-1 ????for (int i = 1; i < n - 1; i++) ????{ ????????// only one condition ????????// will be true at a?? ????????// time either a[i]? ????????// will be greater than ????????// neighbours or less? ????????// than neighbours ????????// check if a[i] is greater ????????// than both its neighbours ????????// then add 1 to x ????????count += (a[i] > a[i - 1] && a[i] > a[i + 1]); ????????// check if a[i] is? ????????// less than both its? ????????// neighbours, then? ????????// add 1 to x ????????count += (a[i] < a[i - 1] && a[i] < a[i + 1]); ????} ????return count; } // driver program int main() { ????int a[] = { 1, 0, 2, 1 }; ????int n = sizeof(a) / sizeof(a[0]); ????cout << extrema(a, n); ????return 0; } ``` ## Java ```java // Java to find? // number of extrema import java.io.*; class GFG { ????// function to find? ????// local extremum ????static int extrema(int a[], int n) ????{ ????????int count = 0; ????????// start loop from? ????????// position 1 till n-1 ????????for (int i = 1; i < n - 1; i++)? ????????{ ????????????// only one condition ????????????// will be true at a?? ????????????// time either a[i]? ????????????// will be greater than ????????????// neighbours or less? ????????????// than neighbours ????????????// check if a[i] is greater ????????????// than both its neighbours ????????????// then add 1 to x ????????????if(a[i] > a[i - 1] && a[i] > a[i + 1]) ????????????????count += 1; ????????????// check if a[i] is? ????????????// less than both its? ????????????// neighbours, then? ????????????// add 1 to x ????????????if(a[i] < a[i - 1] && a[i] < a[i + 1]) ????????????????count += 1; ????????} ????????return count; ????} ????// driver program ????public static void main(String args[]) ????????????????????????????throws IOException ????{ ????????int a[] = { 1, 0, 2, 1 }; ????????int n = a.length; ????????System.out.println(extrema(a, n)); ????} } /* This code is contributed by Nikita Tiwari.*/ ``` ## Python3 ```py # Python 3 to find # number of extrema # function to find # local extremum def extrema(a, n): ????count = 0 ????# start loop from ????# position 1 till n-1 ????for i in range(1, n - 1) : ????????# only one condition? ????????# will be true? ????????# at a time either ????????# a[i] will be greater? ????????# than neighbours or? ????????# less than neighbours ????????# check if a[i] if ????????# greater than both its? ????????# neighbours, then add? ????????# 1 to x ????????count += (a[i] > a[i - 1] and a[i] > a[i + 1]); ????????# check if a[i] if ????????# less than both its? ????????# neighbours, then? ????????# add 1 to x ????????count += (a[i] < a[i - 1] and a[i] < a[i + 1]); ????return count # driver program a = [1, 0, 2, 1 ] n = len(a) print(extrema(a, n)) # This code is contributed by Smitha Dinesh Semwal ``` ## C# ```cs // C# to find? // number of extrema using System; class GFG { ????// function to find? ????// local extremum ????static int extrema(int []a, int n) ????{ ????????int count = 0; ????????// start loop from? ????????// position 1 till n-1 ????????for (int i = 1; i < n - 1; i++)? ????????{ ????????????// only one condition ????????????// will be true at a? ????????????// time either a[i]? ????????????// will be greater than ????????????// neighbours or less? ????????????// than neighbours ????????????// check if a[i] is greater ????????????// than both its neighbours ????????????// then add 1 to x ????????????if(a[i] > a[i - 1] && a[i] > a[i + 1]) ????????????????count += 1; ????????????// check if a[i] is? ????????????// less than both its? ????????????// neighbours, then? ????????????// add 1 to x ????????????if(a[i] < a[i - 1] && a[i] < a[i + 1]) ????????????????count += 1; ????????} ????????return count; ????} ????// Driver program ????public static void Main() ????{ ????????int []a = { 1, 0, 2, 1 }; ????????int n = a.Length; ????Console.WriteLine(extrema(a, n)); ????} } /* This code is contributed by vt_m.*/ ``` ## PHP ```php <?php // PHP to find number // of extrema // function to find? // local extremum function extrema($a, $n) { ????$count = 0; ????// start loop from position 1 ????// till n-1 ????for ($i = 1; $i < $n - 1; $i++) ????{ ????????// only one condition ????????// will be true at a? ????????// time either a[i]? ????????// will be greater than ????????// neighbours or less? ????????// than neighbours ????????// check if a[i] is greater ????????// than both its neighbours ????????// then add 1 to x ????????$count += ($a[$i] > $a[$i - 1] and? ???????????????????$a[$i] > $a[$i + 1]); ????????// check if a[i] is? ????????// less than both its? ????????// neighbours, then? ????????// add 1 to x ????????$count += ($a[$i] < $a[$i - 1] and? ???????????????????$a[$i] < $a[$i + 1]); ????} ????return $count; } // Driver Code $a = array( 1, 0, 2, 1 ); $n = count($a); echo extrema($a, $n); // This code is contributed by anuj_67\. ?> ``` **輸出**: ``` 2 ``` * * * * * *
                  <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>

                              哎呀哎呀视频在线观看