<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>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # 兩個元素之間的最大差異,使得較大的元素出現在較小的數字之后 > 原文: [https://www.geeksforgeeks.org/maximum-difference-between-two-elements/](https://www.geeksforgeeks.org/maximum-difference-between-two-elements/) 給定整數數組`arr[]`,找出任意兩個元素之間的最大差值,以使較大的元素出現在較小的數字之后。 **示例**: ``` Input : arr = {2, 3, 10, 6, 4, 8, 1} Output : 8 Explanation : The maximum difference is between 10 and 2. Input : arr = {7, 9, 5, 6, 3, 2} Output : 2 Explanation : The maximum difference is between 9 and 7. ``` **方法 1(簡單)** 使用兩個循環。 在外循環中,拾取元素一個接一個,在內循環中,計算拾取的元素與數組中每個其他元素的差,并將該差與到目前為止計算出的最大差進行比較。 下面是上述方法的實現: ## C++ ```cpp // C++ program to find Maximum difference? // between two elements such that larger? // element appears after the smaller number #include <bits/stdc++.h> using namespace std; /* The function assumes that there are? ???at least two elements in array. The? ???function returns a negative value if the ???array is sorted in decreasing order and?? ???returns 0 if elements are equal */ int maxDiff(int arr[], int arr_size) {????? ??int max_diff = arr[1] - arr[0]; ??for (int i = 0; i < arr_size; i++) ??{ ????for (int j = i+1; j < arr_size; j++) ????{????? ??????if (arr[j] - arr[i] > max_diff)? ????????max_diff = arr[j] - arr[i]; ????}? ??}????????? ??return max_diff; }? /* Driver program to test above function */ int main() { ??int arr[] = {1, 2, 90, 10, 110}; ??int n = sizeof(arr) / sizeof(arr[0]); ??// Function calling ??cout << "Maximum difference is " << maxDiff(arr, n); ??return 0; } ``` ## C ``` #include<stdio.h> /* The function assumes that there are at least two ???elements in array. ???The function returns a negative value if the array is ???sorted in decreasing order.? ???Returns 0 if elements are equal */ int maxDiff(int arr[], int arr_size) {????? ??int max_diff = arr[1] - arr[0]; ??int i, j; ??for (i = 0; i < arr_size; i++) ??{ ????for (j = i+1; j < arr_size; j++) ????{???????? ??????if (arr[j] - arr[i] > max_diff)??? ?????????max_diff = arr[j] - arr[i]; ????}???? ??}?????????? ??return max_diff; }???? /* Driver program to test above function */ int main() { ??int arr[] = {1, 2, 90, 10, 110}; ??printf("Maximum difference is %d",? maxDiff(arr, 5)); ??getchar(); ??return 0; } ``` ## Java ```java // Java program to find Maximum difference? // between two elements such that larger? // element appears after the smaller number class MaximumDiffrence? { ????/* The function assumes that there are at least two ???????elements in array. ???????The function returns a negative value if the array is ???????sorted in decreasing order.? ???????Returns 0 if elements are equal */ ????int maxDiff(int arr[], int arr_size)? ????{ ????????int max_diff = arr[1] - arr[0]; ????????int i, j; ????????for (i = 0; i < arr_size; i++)? ????????{ ????????????for (j = i + 1; j < arr_size; j++)? ????????????{ ????????????????if (arr[j] - arr[i] > max_diff) ????????????????????max_diff = arr[j] - arr[i]; ????????????} ????????} ????????return max_diff; ????} ????/* Driver program to test above functions */ ????public static void main(String[] args)? ????{ ????????MaximumDiffrence maxdif = new MaximumDiffrence(); ????????int arr[] = {1, 2, 90, 10, 110}; ????????System.out.println("Maximum differnce is " +? ????????????????????????????????maxdif.maxDiff(arr, 5)); ????} } // This code has been contributed by Mayank Jaiswal ``` ## Python3 ```py # Python 3 code to find Maximum difference # between two elements such that larger? # element appears after the smaller number # The function assumes that there are at? # least two elements in array. The function? # returns a negative value if the array is # sorted in decreasing order. Returns 0? # if elements are equal def maxDiff(arr, arr_size): ????max_diff = arr[1] - arr[0] ????for i in range( 0, arr_size ): ????????for j in range( i+1, arr_size ): ????????????if(arr[j] - arr[i] > max_diff):? ????????????????max_diff = arr[j] - arr[i] ????return max_diff # Driver program to test above function? arr = [1, 2, 90, 10, 110] size = len(arr) print ("Maximum difference is", maxDiff(arr, size)) # This code is contributed by Swetank Modi ``` ## C# ```cs // C# code to find Maximum difference using System; class GFG { ????// The function assumes that there? ????// are at least two elements in array. ????// The function returns a negative? ????// value if the array is sorted in? ????// decreasing order. Returns 0 if ????// elements are equal? ????static int maxDiff(int[] arr, int arr_size) ????{ ????????int max_diff = arr[1] - arr[0]; ????????int i, j; ????????for (i = 0; i < arr_size; i++) { ????????????for (j = i + 1; j < arr_size; j++) { ????????????????if (arr[j] - arr[i] > max_diff) ????????????????????max_diff = arr[j] - arr[i]; ????????????} ????????} ????????return max_diff; ????} ????// Driver code ????public static void Main() ????{ ????????int[] arr = { 1, 2, 90, 10, 110 }; ????????Console.Write("Maximum differnce is " +? ????????????????????????????????maxDiff(arr, 5)); ????} } // This code is contributed by Sam007 ``` ## PHP ```php <?php // PHP program to find Maximum difference? // between two elements such that larger? // element appears after the smaller number /* The function assumes that there are? at least two elements in array. The? function returns a negative value if the array is sorted in decreasing order and? returns 0 if elements are equal */ function maxDiff($arr, $arr_size) {? $max_diff = $arr[1] - $arr[0]; for ($i = 0; $i < $arr_size; $i++) { ????for ($j = $i+1; $j < $arr_size; $j++) ????{? ????if ($arr[$j] - $arr[$i] > $max_diff)? ????????$max_diff = $arr[$j] - $arr[$i]; ????}? }????? return $max_diff; }? // Driver Code $arr = array(1, 2, 90, 10, 110); $n = sizeof($arr); // Function calling echo "Maximum difference is " .? ?????????????maxDiff($arr, $n); // This code is contributed? // by Akanksha Rai(Abby_akku) ``` **輸出**: ``` Maximum difference is 109 ``` **時間復雜度**:`O(n ^ 2)` **輔助空間**:`O(1)` **方法 2(技巧和效率)** 在此方法中,我們采用已找到的最小元素作為差異,而不是采用每個元素之間的差異。 因此,我們需要跟蹤 2 件事情: 1)到目前為止找到的最大差異(`max_diff`)。 2)到目前為止訪問的最小人數(`min_element`)。 ## C++ ``` // C++ program to find Maximum difference? // between two elements such that larger? // element appears after the smaller number #include <bits/stdc++.h> using namespace std; /* The function assumes that there are? ???at least two elements in array. The? ???function returns a negative value if the ???array is sorted in decreasing order and?? ???returns 0 if elements are equal */ int maxDiff(int arr[], int arr_size) { ?????// Maximum difference found so far ?????int max_diff = arr[1] - arr[0]; ?????// Minimum number visited so far? ?????int min_element = arr[0]; ?????for(int i = 1; i < arr_size; i++) ?????{????? ???????if (arr[i] - min_element > max_diff)????????????????????????????? ???????max_diff = arr[i] - min_element; ???????if (arr[i] < min_element) ???????min_element = arr[i];????????????????????? ?????} ?????return max_diff; }? /* Driver program to test above function */ int main() { ??int arr[] = {1, 2, 90, 10, 110}; ??int n = sizeof(arr) / sizeof(arr[0]); ??// Function calling ??cout << "Maximum difference is " << maxDiff(arr, n); ??return 0; } ``` ## C ``` #include<stdio.h> /* The function assumes that there are at least two elements in array. The function returns a negative value if the array is sorted in decreasing order. Returns 0 if elements are equal */ int maxDiff(int arr[], int arr_size) { int max_diff = arr[1] - arr[0]; int min_element = arr[0]; int i; for(i = 1; i < arr_size; i++) {????? ????if (arr[i] - min_element > max_diff)????????????????????????????? ????max_diff = arr[i] - min_element; ????if (arr[i] < min_element) ????????min_element = arr[i];????????????????????? } return max_diff; }? /* Driver program to test above function */ int main() { int arr[] = {1, 2, 6, 80, 100}; int size = sizeof(arr)/sizeof(arr[0]); printf("Maximum difference is %d", maxDiff(arr, size)); getchar(); return 0; } ``` ## Java ```java // Java program to find Maximum difference? // between two elements such that larger? // element appears after the smaller number class MaximumDiffrence? { ????/* The function assumes that there are at least two ???????elements in array. ???????The function returns a negative value if the array is ???????sorted in decreasing order. ???????Returns 0 if elements are equal? */ ????int maxDiff(int arr[], int arr_size)? ????{ ????????int max_diff = arr[1] - arr[0]; ????????int min_element = arr[0]; ????????int i; ????????for (i = 1; i < arr_size; i++)? ????????{ ????????????if (arr[i] - min_element > max_diff) ????????????????max_diff = arr[i] - min_element; ????????????if (arr[i] < min_element) ????????????????min_element = arr[i]; ????????} ????????return max_diff; ????} ????/* Driver program to test above functions */ ????public static void main(String[] args)? ????{ ????????MaximumDiffrence maxdif = new MaximumDiffrence(); ????????int arr[] = {1, 2, 90, 10, 110}; ????????int size = arr.length; ????????System.out.println("MaximumDifference is " +? ????????????????????????????????maxdif.maxDiff(arr, size)); ????} } // This code has been contributed by Mayank Jaiswal ``` ## Python3 ```py # Python 3 code to find Maximum difference # between two elements such that larger? # element appears after the smaller number # The function assumes that there are? # at least two elements in array. # The function returns a negative? # value if the array is sorted in? # decreasing order. Returns 0 if? # elements are equal def maxDiff(arr, arr_size): ????max_diff = arr[1] - arr[0] ????min_element = arr[0] ????for i in range( 1, arr_size ): ????????if (arr[i] - min_element > max_diff): ????????????max_diff = arr[i] - min_element ????????if (arr[i] < min_element): ????????????min_element = arr[i] ????return max_diff # Driver program to test above function? arr = [1, 2, 6, 80, 100] size = len(arr) print ("Maximum difference is",? ????????maxDiff(arr, size)) # This code is contributed by Swetank Modi ``` ## C# ``` // C# code to find Maximum difference using System; class GFG { ????// The function assumes that there? ????// are at least two elements in array. ????// The function returns a negative? ????// value if the array is sorted in ????// decreasing order.Returns 0 if? ????// elements are equal? ????static int maxDiff(int[] arr, int arr_size) ????{ ????????int max_diff = arr[1] - arr[0]; ????????int min_element = arr[0]; ????????int i; ????????for (i = 1; i < arr_size; i++) { ????????????if (arr[i] - min_element > max_diff) ????????????????max_diff = arr[i] - min_element; ????????????if (arr[i] < min_element) ????????????????min_element = arr[i]; ????????} ????????return max_diff; ????} ????// Driver code ????public static void Main() ????{ ????????int[] arr = { 1, 2, 90, 10, 110 }; ????????int size = arr.Length; ????????Console.Write("MaximumDifference is " + ???????????????????????????????maxDiff(arr, size)); ????} } // This code is contributed by Sam007 ``` ## PHP ```php <?php // PHP program to find Maximum? // difference between two elements? // such that larger element appears // after the smaller number // The function assumes that there? // are at least two elements in array.? // The function returns a negative? // value if the array is sorted in? // decreasing order and returns 0? // if elements are equal?? function maxDiff($arr, $arr_size) { ????// Maximum difference found so far ????$max_diff = $arr[1] - $arr[0]; ????// Minimum number visited so far? ????$min_element = $arr[0]; ????for($i = 1; $i < $arr_size; $i++) ????{????? ????if ($arr[$i] - $min_element > $max_diff)????????????????????????????? ????$max_diff = $arr[$i] - $min_element; ????if ($arr[$i] < $min_element) ????$min_element = $arr[$i];????????????????????? ????} ????return $max_diff; } // Driver Code $arr = array(1, 2, 90, 10, 110); $n = count($arr); // Function calling echo "Maximum difference is " . ?????????????maxDiff($arr, $n); // This code is contributed by Sam007 ?> ``` **輸出**: ``` Maximum difference is 109 ``` **時間復雜度**:`O(n)` **輔助空間**:`O(1)` 像`min`元素一樣,我們也可以從右側跟蹤`max`元素。 **感謝 Katamaran 建議這種方法。** 下面是實現: ## C++ ``` // C++ program to find Maximum difference? // between two elements such that larger? // element appears after the smaller number #include <bits/stdc++.h> using namespace std; /* The function assumes that there are? ???at least two elements in array. The? ???function returns a negative value if the ???array is sorted in decreasing order and?? ???returns 0 if elements are equal */ int maxDiff(int arr[], int n) { ????// Initialize Result ????int maxDiff = -1;? ????// Initialize max element from right side ????int maxRight = arr[n-1];? ????for (int i = n-2; i >= 0; i--) ????{ ????????if (arr[i] > maxRight) ????????????maxRight = arr[i]; ????????else ????????{ ????????????int diff = maxRight - arr[i]; ????????????if (diff > maxDiff) ????????????{ ????????????????maxDiff = diff; ????????????} ????????} ????} ????return maxDiff; } /* Driver program to test above function */ int main() { ??int arr[] = {1, 2, 90, 10, 110}; ??int n = sizeof(arr) / sizeof(arr[0]); ??// Function calling ??cout << "Maximum difference is " << maxDiff(arr, n); ??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>

                              哎呀哎呀视频在线观看