<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/union-and-intersection-of-two-sorted-arrays-2/](https://www.geeksforgeeks.org/union-and-intersection-of-two-sorted-arrays-2/) 給定兩個排序的數組,找到它們的并集和交集。 **示例**: ``` Input : arr1[] = {1, 3, 4, 5, 7} arr2[] = {2, 3, 5, 6} Output : Union : {1, 2, 3, 4, 5, 6, 7} Intersection : {3, 5} Input : arr1[] = {2, 5, 6} arr2[] = {4, 6, 8, 10} Output : Union : {2, 4, 5, 6, 8, 10} Intersection : {6} ``` [](https://practice.geeksforgeeks.org/problem-page.php?pid=537) ## 強烈建議您在繼續解決方案之前,單擊此處進行練習。 **數組 arr1 []和 arr2 []的并集** 要查找兩個排序數組的并集,請遵循以下合并過程: > 1)使用兩個索引變量 i 和 j,初始值 i = 0,j = 0 > 2)如果 arr1 [i]小于 arr2 [j],則打印 arr1 [i]并遞增 i。 > 3)如果 arr1 [i]大于 arr2 [j],則打印 arr2 [j]并遞增 j。 > 4)如果兩者相同,則打印其中的任何一個并遞增 i 和 j。 > 5)打印較大數組的其余元素。 下面是上述方法的實現: ## C++ ```cpp // C++ program to find union of // two sorted arrays #include <bits/stdc++.h> using namespace std; /* Function prints union of arr1[] and arr2[] ???m is the number of elements in arr1[] ???n is the number of elements in arr2[] */ int printUnion(int arr1[], int arr2[], int m, int n) { ??int i = 0, j = 0; ??while (i < m && j < n) ??{ ????if (arr1[i] < arr2[j]) ???????cout << arr1[i++] << " "; ????else if (arr2[j] < arr1[i]) ???????cout << arr2[j++] << " "; ????else ????{ ???????cout << arr2[j++] << " "; ???????i++; ????} ??} ??/* Print remaining elements of the larger array */ ??while(i < m) ?????cout << arr1[i++] << " "; ??while(j < n) ????cout << arr2[j++] << " "; } /* Driver program to test above function */ int main() { ??int arr1[] = {1, 2, 4, 5, 6}; ??int arr2[] = {2, 3, 5, 7}; ??int m = sizeof(arr1)/sizeof(arr1[0]); ??int n = sizeof(arr2)/sizeof(arr2[0]); ??// Function calling ??printUnion(arr1, arr2, m, 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>

                              哎呀哎呀视频在线观看