<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/shortest-un-ordered-subarray/](https://www.geeksforgeeks.org/shortest-un-ordered-subarray/) 給定一個數組的 n 個長度,問題是我們必須找到給定數組中最短的無序{既不增加也不減少}子數組的長度。 例子: ``` Input : n = 5 7 9 10 8 11 Output : 3 Explanation : 9 10 8 unordered sub array. Input : n = 5 1 2 3 4 5 Output : 0 Explanation : Array is in increasing order. ``` 這個想法基于以下事實:最短子數組的大小可以為 0 或 3。我們必須檢查數組元素是增加還是減少,如果所有數組元素都在增加或減少,則最短子數組的長度為 0, 而且,如果數組元素中的任何一個都不跟隨遞增或遞減,則其最短長度為 3。 ## C++ ```cpp // CPP program to find shortest subarray which is // unsorted. #include <bits/stdc++.h> using namespace std; // bool function for checking an array elements? // are in increasing. bool increasing(int a[], int n) { ????for (int i = 0; i < n - 1; i++)? ????????if (a[i] >= a[i + 1]) ????????????return false;???? ????return true; } // bool function for checking an array? // elements are in decreasing. bool decreasing(int a[], int n) { ????for (int i = 0; i < n - 1; i++)? ????????if (a[i] < a[i + 1]) ????????????return false;???? ????return true; } int shortestUnsorted(int a[], int n) { ????// increasing and decreasing are two functions. ????// if function return true value then print ????// 0 otherwise 3\. ????if (increasing(a, n) == true || ???????decreasing(a, n) == true) ????????return 0; ????else ????????return 3; } // Driver code int main() { ????int ar[] = { 7, 9, 10, 8, 11 }; ????int n = sizeof(ar) / sizeof(ar[0]); ????cout << shortestUnsorted(ar, n); ????return 0; } ``` ## Java ```java // JAVA program to find shortest subarray which is // unsorted. import java.util.*; import java.io.*; class GFG { ????// boolean function to check array elements? ????// are in increasing order or not ????public static boolean increasing(int a[],int n) ????{ ????????for (int i = 0; i < n - 1; i++)? ????????????if (a[i] >= a[i + 1]) ????????????????return false;? ????????return true; ????} ????// boolean function to check array elements? ????// are in decreasing order or not ????public static boolean decreasing(int arr[],int n) ????{ ????????for (int i = 0; i < n - 1; i++)? ????????????if (arr[i] < arr[i + 1]) ????????????????return false;? ????????return true; ????} ????public static int shortestUnsorted(int a[],int n) ????{ ????????// increasing and decreasing are two functions. ????????// if function return true value then print ????????// 0 otherwise 3\. ????????if (increasing(a, n) == true ||? ?????????????????????????????decreasing(a, n) == true) ????????????return 0; ????????else ????????????return 3; ????} ????// driver program ????public static void main (String[] args) { ????????int ar[] = new int[]{7, 9, 10, 8, 11}; ????????int n = ar.length; ????????System.out.println(shortestUnsorted(ar,n)); ????} } // This code is contributed by Akash Singh. ``` ## Python3 ```py # Python3 program to find shortest? # subarray which is unsorted # Bool function for checking an array?? # elements are in increasing def increasing(a, n): ????for i in range(0, n - 1):? ????????if (a[i] >= a[i + 1]): ????????????return False ????return True # Bool function for checking an array? # elements are in decreasing def decreasing(a, n): ????for i in range(0, n - 1):? ????????if (a[i] < a[i + 1]): ????????????return False ????return True def shortestUnsorted(a, n): ????# increasing and decreasing are two functions. ????# if function return True value then print ????# 0 otherwise 3\. ????if (increasing(a, n) == True or ????????decreasing(a, n) == True): ????????return 0 ????else: ????????return 3 # Driver code ar = [7, 9, 10, 8, 11]? n = len(ar)? print(shortestUnsorted(ar, n)) # This code is contributed by Smitha Dinesh Semwal.? ``` ## C# ```cs // Program to find the shortest // subarray which is unsorted. using System; class GFG { ????// boolean function to check ????// array elements are in the ????// increasing order or not ????public static bool increasing(int[] a, int n) ????{ ????????for (int i = 0; i < n - 1; i++) ????????????if (a[i] >= a[i + 1]) ????????????????return false; ????????return true; ????} ????// boolean function to check ????// array elements are in the ????// decreasing order or not ????public static bool decreasing(int[] arr, int n) ????{ ????????for (int i = 0; i < n - 1; i++) ????????????if (arr[i] < arr[i + 1]) ????????????????return false; ????????return true; ????} ????public static int shortestUnsorted(int[] a, int n) ????{ ????????// increasing and decreasing are ????????// two functions. function return ????????// true value then print 0 else 3 ?????????if (increasing(a, n) == true || ?????????????decreasing(a, n) == true) ????????????return 0; ????????else ????????????return 3; ????} ????// Driver program ????public static void Main() ????{ ????????int[] ar = new int[] { 7, 9, 10, 8, 11 }; ????????int n = ar.Length; ????????Console.WriteLine(shortestUnsorted(ar, n)); ????} } // This code is contributed by vt_m. ``` ## PHP ```php <?php // php program to find shortest? // subarray which is unsorted. // bool function for checking an? // array elements are in increasing. function increasing($a, $n) { ????for ( $i = 0; $i < $n - 1; $i++)? ????????if ($a[$i] >= $a[$i + 1]) ????????????return false; ????return true; } // bool function for checking an? // array elements are in decreasing. function decreasing($a, $n) { ????for ($i = 0; $i < $n - 1; $i++)? ????????if ($a[$i] < $a[$i + 1]) ????????????return false;? ????return true; } function shortestUnsorted($a, $n) { ????// increasing and decreasing are ????// two functions. if function? ????// return true value then print ????// 0 otherwise 3\. ????if (increasing($a, $n) == true || ??????????decreasing($a, $n) == true) ????????return 0; ????else ????????return 3; } // Driver code $ar = array( 7, 9, 10, 8, 11 ); $n = sizeof($ar); echo shortestUnsorted($ar, $n); // This code is contributed by // nitin mittal. ?> ``` **輸出**: ``` 3 ``` * * * * * *
                  <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>

                              哎呀哎呀视频在线观看