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

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # 找到一個三元組,使得兩個和等于第三元素 > 原文: [https://www.geeksforgeeks.org/find-triplet-sum-two-equals-third-element/](https://www.geeksforgeeks.org/find-triplet-sum-two-equals-third-element/) 給定一個整數數組,您必須找到三個數字,以使兩個元素的總和等于第三個元素。 **示例**: ``` Input : {5, 32, 1, 7, 10, 50, 19, 21, 2} Output : 21, 2, 19 Input : {5, 32, 1, 7, 10, 50, 19, 21, 0} Output : no such triplet exist ``` 問題源: [Arcesium 面試體驗| 第七組(在校園實習)](https://www.geeksforgeeks.org/arcesium-interview-experience-set-7-campus-internship/) **簡單方法**:運行三個循環,檢查是否存在三元組,以使兩個元素的總和等于第三個元素。 **時間復雜度**:O(n ^ 3) **高效方法**:這個想法類似于[。](https://www.geeksforgeeks.org/find-a-triplet-that-sum-to-a-given-value/) * 首先對給定的數組進行排序。 * 從后面開始固定三個中的最大元素,并遍歷數組以找到其他兩個數字,它們合計為第三個元素。 * 取兩個指針 j(從前面開始)和 k(最初為 i-1)以找到兩個數字中的最小值,并從 i-1 中找到兩個剩余數字中的最大值 * 如果兩個數字的和仍小于 A [i],則需要增加兩個數字之和的值,從而增加 j 指針,從而增加 **A [j]的值 + A [k]** 。 * 如果兩個數字的總和大于 A [i],則我們需要減小兩個數字之和的值,從而減小 k 指針,從而減小 **A [j] +的總值 A [k]** 。 下圖是上述方法的模擬: ![](https://img.kancloud.cn/d0/dc/d0dc13116526a7dc4793967bcd97c67e_1000x792.png) 下面是上述方法的實現: ## C++ ```cpp // CPP program to find three numbers // such that sum of two makes the // third element in array #include <bits/stdc++.h> using namespace std; // Utility function for finding // triplet in array void findTriplet(int arr[], int n) { ????// sort the array ????sort(arr, arr + n); ????// for every element in arr ????// check if a pair exist(in array) whose ????// sum is equal to arr element ????for (int i = n - 1; i >= 0; i--) { ????????int j = 0; ????????int k = i - 1; ????????// Iterate forward and backward to find ????????// the other two elements ????????while (j < k) { ????????????// If the two elements sum is ????????????// equal to the third element ????????????if (arr[i] == arr[j] + arr[k]) { ????????????????// pair found ????????????????cout << "numbers are " << arr[i] << " " ?????????????????????<< arr[j] << " " << arr[k] << endl; ????????????????return; ????????????} ????????????// If the element is greater than ????????????// sum of both the elements, then try ????????????// adding a smaller number to reach the ????????????// equality ????????????else if (arr[i] > arr[j] + arr[k]) ????????????????j += 1; ????????????// If the element is smaller, then ????????????// try with a smaller number ????????????// to reach equality, so decrease K ????????????else ????????????????k -= 1; ????????} ????} ????// No such triplet is found in array ????cout << "No such triplet exists"; } // driver program int main() { ????int arr[] = { 5, 32, 1, 7, 10, 50, 19, 21, 2 }; ????int n = sizeof(arr) / sizeof(arr[0]); ????findTriplet(arr, n); ????return 0; } ``` ## Java ```java // Java program to find three numbers // such that sum of two makes the // third element in array import java.util.Arrays; public class GFG { ????// utility function for finding ????// triplet in array ????static void findTriplet(int arr[], int n) ????{ ????????// sort the array ????????Arrays.sort(arr); ????????// for every element in arr ????????// check if a pair exist(in array) whose ????????// sum is equal to arr element ????????for (int i = n - 1; i >= 0; i--) { ????????????int j = 0; ????????????int k = i - 1; ????????????while (j < k) { ????????????????if (arr[i] == arr[j] + arr[k]) { ????????????????????// pair found ????????????????????System.out.println("numbers are " + arr[i] + " " ???????????????????????????????????????+ arr[j] + " " + arr[k]); ????????????????????return; ????????????????} ????????????????else if (arr[i] > arr[j] + arr[k]) ????????????????????j += 1; ????????????????else ????????????????????k -= 1; ????????????} ????????} ????????// no such triplet is found in array ????????System.out.println("No such triplet exists"); ????} ????// driver program ????public static void main(String args[]) ????{ ????????int arr[] = { 5, 32, 1, 7, 10, 50, 19, 21, 2 }; ????????int n = arr.length; ????????findTriplet(arr, n); ????} } // This code is contributed by Sumit Ghosh ``` ## Python ``` # Python program to find three numbers # such that sum of two makes the # third element in array # utility function for finding # triplet in array def findTriplet(arr, n): ????# sort the array ????arr.sort() ????# for every element in arr ????# check if a pair exist(in array) whose ????# sum is equal to arr element ????i = n - 1 ????while(i >= 0): ????????j = 0 ????????k = i - 1 ????????while (j < k): ????????????if (arr[i] == arr[j] + arr[k]): ????????????????# pair found ????????????????print "numbers are ", arr[i], arr[j], arr[k] ????????????????return ????????????elif (arr[i] > arr[j] + arr[k]): ????????????????j += 1 ????????????else: ????????????????k -= 1 ????????i -= 1 ????# no such triplet is found in array ????print "No such triplet exists" # driver program arr = [ 5, 32, 1, 7, 10, 50, 19, 21, 2 ] n = len(arr) findTriplet(arr, n) # This code is contributed by Sachin Bisht ``` ## C# ```cs // C# program to find three numbers // such that sum of two makes the // third element in array using System; public class GFG { ????// utility function for finding ????// triplet in array ????static void findTriplet(int[] arr, int n) ????{ ????????// sort the array ????????Array.Sort(arr); ????????// for every element in arr ????????// check if a pair exist(in ????????// array) whose sum is equal ????????// to arr element ????????for (int i = n - 1; i >= 0; i--) { ????????????int j = 0; ????????????int k = i - 1; ????????????while (j < k) { ????????????????if (arr[i] == arr[j] + arr[k]) { ????????????????????// pair found ????????????????????Console.WriteLine("numbers are " ??????????????????????????????????????+ arr[i] + " " + arr[j] ??????????????????????????????????????+ " " + arr[k]); ????????????????????return; ????????????????} ????????????????else if (arr[i] > arr[j] + arr[k]) ????????????????????j += 1; ????????????????else ????????????????????k -= 1; ????????????} ????????} ????????// no such triplet is found in array ????????Console.WriteLine("No such triplet exists"); ????} ????// driver program ????public static void Main() ????{ ????????int[] arr = { 5, 32, 1, 7, 10, 50, ??????????????????????19, 21, 2 }; ????????int n = arr.Length; ????????findTriplet(arr, n); ????} } // This code is contributed by vt_m. ``` ## PHP ```php <?php // PHP program to find three // numbers such that sum of? // two makes the third // element in array // utility function for? // finding triplet in array function findTriplet($arr, $n) { ????// sort the array ????sort($arr); ????// for every element in? ????// arr check if a pair? ????// exist(in array) whose ????// sum is equal to arr element ????for ($i = $n - 1; $i >= 0; $i--) ????{ ????????$j = 0; ????????$k = $i - 1; ????????while ($j < $k)? ????????{ ????????????if ($arr[$i] == $arr[$j] + $arr[$k])? ????????????{ ????????????????// pair found ????????????????echo "numbers are ", $arr[$i], " ",? ??????????????????????????????????????$arr[$j], " ",? ??????????????????????????????????????$arr[$k]; ????????????????return; ????????????}? ????????????else if ($arr[$i] > $arr[$j] +? ????????????????????????????????$arr[$k]) ????????????????$j += 1; ????????????else ????????????????$k -= 1; ????????} ????} ????// no such triplet? ????// is found in array ????echo "No such triplet exists"; } // Driver Code $arr = array(5, 32, 1, 7, 10,? ?????????????50, 19, 21, 2 ); $n = count($arr); findTriplet($arr, $n); // This code is contributed by anuj_67\. ?> ``` **輸出**: ``` numbers are 21 2 19 ``` **時間復雜度**:O(N ^ 2) 本文由 [**Mandeep Singh**](https://github.com/msdeep14) 貢獻。 如果您喜歡 GeeksforGeeks 并希望做出貢獻,則還可以使用 [tribution.geeksforgeeks.org](http://www.contribute.geeksforgeeks.org) 撰寫文章,或將您的文章郵寄至 tribution@geeksforgeeks.org。 查看您的文章出現在 GeeksforGeeks 主頁上,并幫助其他 Geeks。
                  <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>

                              哎呀哎呀视频在线观看