<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/check-exist-two-elements-array-whose-sum-equal-sum-rest-array/](https://www.geeksforgeeks.org/check-exist-two-elements-array-whose-sum-equal-sum-rest-array/) 我們有一個整數數組,我們必須在數組中找到兩個這樣的元素,以使這兩個元素的總和等于數組中其余元素的總和。 **示例**: ``` Input : arr[] = {2, 11, 5, 1, 4, 7} Output : Elements are 4 and 11 Note that 4 + 11 = 2 + 5 + 1 + 7 Input : arr[] = {2, 4, 2, 1, 11, 15} Output : Elements do not exist ``` **簡單解決方案**是逐對考慮每一對,找到其總和,然后將總和與其余元素的總和進行比較。 如果找到一對總和等于其余元素的對,則打印該對并返回 true。 該解決方案的時間復雜度為 O(n <sup>3</sup> ) 一種有效的**解決方案**是找到所有數組元素的總和。 將該總和設為“和”。 現在,任務簡化為找到總和等于 sum / 2 的一對。 另一個優化方法是,只有在整個數組的和為偶數的情況下,才可以存在一對,因為我們基本上將它分成相等的兩個部分。 **1-** 查找整個數組的總和。 將該總和設為“總和”。 **2-** 如果總和為奇數,則返回 false。 **3-** 使用此處討論的基于散列的方法[在此處](https://www.geeksforgeeks.org/write-a-c-program-that-given-a-set-a-of-n-numbers-and-another-number-x-determines-whether-or-not-there-exist-two-elements-in-s-whose-sum-is-exactly-x/)作為方法 2,找到總和等于“ sum / 2”的貨幣對。如果找到一對,請打印并返回 true 。 **4-** 如果不存在任何對,則返回 false。 下面是上述步驟的實現。 ## C++ ```cpp // C++ program to find whether two elements exist // whose sum is equal to sum of rest of the elements. #include<bits/stdc++.h> using namespace std; // Function to check whether two elements exist // whose sum is equal to sum of rest of the elements. bool checkPair(int arr[],int n) { ????// Find sum of whole array ????int sum = 0; ????for (int i=0; i<n; i++) ????????sum += arr[i]; ????// If sum of array is not even than we can not ????// divide it into two part ????if (sum%2 != 0) ????????return false; ????sum = sum/2; ????// For each element arr[i], see if there is ????// another element with vaalue sum - arr[i] ????unordered_set<int> s; ????for (int i=0; i<n; i++) ????{ ????????int val = sum-arr[i]; ????????// If element exist than return the pair ????????if (s.find(val) != s.end()) ????????{ ????????????printf("Pair elements are %d and %d\n", ????????????????????????????????????arr[i], val); ????????????return true; ????????} ????????s.insert(arr[i]); ????} ????return false; } // Driver program. int main() { ????int arr[] = {2, 11, 5, 1, 4, 7}; ????int n = sizeof(arr)/sizeof(arr[0]); ????if (checkPair(arr, n) == false) ???????printf("No pair found"); ????return 0; } ``` ## Java ```java // Java program to find whether two elements exist // whose sum is equal to sum of rest of the elements. import java.util.*; class GFG? { ????// Function to check whether two elements exist ????// whose sum is equal to sum of rest of the elements. ????static boolean checkPair(int arr[], int n) ????{ ????????// Find sum of whole array ????????int sum = 0; ????????for (int i = 0; i < n; i++) ????????{ ????????????sum += arr[i]; ????????} ????????// If sum of array is not even than we can not ????????// divide it into two part ????????if (sum % 2 != 0)? ????????{ ????????????return false; ????????} ????????sum = sum / 2; ????????// For each element arr[i], see if there is ????????// another element with vaalue sum - arr[i] ????????HashSet<Integer> s = new HashSet<Integer>(); ????????for (int i = 0; i < n; i++) ????????{ ????????????int val = sum - arr[i]; ????????????// If element exist than return the pair ????????????if (s.contains(val) &&? ????????????????val == (int) s.toArray()[s.size() - 1])? ????????????{ ????????????????System.out.printf("Pair elements are %d and %d\n", ????????????????????????arr[i], val); ????????????????return true; ????????????} ????????????s.add(arr[i]); ????????} ????????return false; ????} ????// Driver program. ????public static void main(String[] args) ????{ ????????int arr[] = {2, 11, 5, 1, 4, 7}; ????????int n = arr.length; ????????if (checkPair(arr, n) == false)? ????????{ ????????????System.out.printf("No pair found"); ????????} ????} } /* This code contributed by PrinciRaj1992 */ ``` ## Python3 ```py # Python3 program to find whether? # two elements exist whose sum is # equal to sum of rest of the elements.? # Function to check whether two? # elements exist whose sum is equal? # to sum of rest of the elements.? def checkPair(arr, n): ????s = set() ????sum = 0 ????# Find sum of whole array? ????for i in range(n): ????????sum += arr[i] ????# / If sum of array is not? ????# even than we can not? ????# divide it into two part? ????if sum % 2 != 0: ????????return False ????sum = sum / 2 ????# For each element arr[i], see if? ????# there is another element with? ????# value sum - arr[i]? ????for i in range(n): ????????val = sum - arr[i] ????????if arr[i] not in s: ????????????s.add(arr[i]) ????????# If element exist than? ????????# return the pair? ????????if val in s: ????????????print("Pair elements are",? ???????????????????arr[i], "and", int(val)) # Driver Code? arr = [2, 11, 5, 1, 4, 7] n = len(arr) if checkPair(arr, n) == False: ????print("No pair found") # This code is contributed? # by Shrikant13 ``` ## C# ```cs // C# program to find whether two elements exist // whose sum is equal to sum of rest of the elements. using System;? using System.Collections.Generic;? class GFG? { ????// Function to check whether two elements exist ????// whose sum is equal to sum of rest of the elements. ????static bool checkPair(int []arr, int n) ????{ ????????// Find sum of whole array ????????int sum = 0; ????????for (int i = 0; i < n; i++) ????????{ ????????????sum += arr[i]; ????????} ????????// If sum of array is not even than we can not ????????// divide it into two part ????????if (sum % 2 != 0)? ????????{ ????????????return false; ????????} ????????sum = sum / 2; ????????// For each element arr[i], see if there is ????????// another element with vaalue sum - arr[i] ????????HashSet<int> s = new HashSet<int>(); ????????for (int i = 0; i < n; i++) ????????{ ????????????int val = sum - arr[i]; ????????????// If element exist than return the pair ????????????if (s.Contains(val)) ????????????{ ????????????????Console.Write("Pair elements are {0} and {1}\n", ????????????????????????arr[i], val); ????????????????return true; ????????????} ????????????s.Add(arr[i]); ????????} ????????return false; ????} ????// Driver code ????public static void Main(String[] args) ????{ ????????int []arr = {2, 11, 5, 1, 4, 7}; ????????int n = arr.Length; ????????if (checkPair(arr, n) == false)? ????????{ ????????????Console.Write("No pair found"); ????????} ????} } // This code contributed by Rajput-Ji ``` ## PHP ```php <?php? // PHP program to find whether two elements exist // whose sum is equal to sum of rest of the elements. // Function to check whether two elements exist // whose sum is equal to sum of rest of the elements. function checkPair(&$arr, $n) { ????// Find sum of whole array ????$sum = 0; ????for ($i = 0; $i < $n; $i++) ????????$sum += $arr[$i]; ????// If sum of array is not even than we? ????// can not divide it into two part ????if ($sum % 2 != 0) ????????return false; ????$sum = $sum / 2; ????// For each element arr[i], see if there is ????// another element with vaalue sum - arr[i] ????$s = array(); ????for ($i = 0; $i < $n; $i++) ????{ ????????$val = $sum - $arr[$i]; ????????// If element exist than return the pair ????????if (array_search($val, $s)) ????????{ ????????????echo "Pair elements are " . $arr[$i] . ?????????????????" and " . $val . "\n"; ????????????return true; ????????} ????????array_push($s, $arr[$i]); ????} ????return false; } // Driver Code $arr = array(2, 11, 5, 1, 4, 7); $n = sizeof($arr); if (checkPair($arr, $n) == false) ????echo "No pair found"; // This code is contributed by ita_c ?> ``` **輸出**: ``` Pair elements are 4 and 11 ``` **時間復雜度**:`O(n)`。 [unordered_set](https://www.geeksforgeeks.org/unorderd_set-stl-uses/) 使用哈希實現。 時間復雜度哈希搜索和插入在此處假定為`O(1)`。
                  <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>

                              哎呀哎呀视频在线观看