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

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 查找一個三元組,將其總和成給定值 > 原文: [https://www.geeksforgeeks.org/find-a-triplet-that-sum-to-a-given-value/](https://www.geeksforgeeks.org/find-a-triplet-that-sum-to-a-given-value/) 給定一個數組和一個值,查找數組中是否有一個三元組,其和等于給定值。 如果數組中存在此類三元組,則打印該三元組并返回 true。 否則返回 false。 **例如**: ``` Input: array = {12, 3, 4, 1, 6, 9}, sum = 24; Output: 12, 3, 9 Explanation: There is a triplet (12, 3 and 9) present in the array whose sum is 24\. Input: array = {1, 2, 3, 4, 5}, sum = 9 Output: 5, 3, 1 Explanation: There is a triplet (5, 3 and 1) present in the array whose sum is 9\. ``` **方法 1** :這是解決上述問題的樸素方法。 * **方法**:一種簡單的方法是生成所有可能的三元組,并將每個三元組的總和與給定值進行比較。 以下代碼使用三個嵌套循環實現此簡單方法。 * **算法**: 1. 給定長度為 *n* 且總和為 *s* 的數組 2. 創建三個嵌套循環,第一個循環從開始到結束(循環計數器 i),第二個循環從 i + 1 到結束(循環計數器 j),第三個循環從 j + 1 到結束(循環計數器 k)運行 3. 這些循環的計數器表示三元組的 3 個元素的索引。 4. 求出 ith,jth 和 kth 元素的總和。 如果總和等于給定的總和。 打印三元組并中斷。 5. 如果沒有三聯體,則打印不存在三聯體。 * **實現**: ## C++ ``` #include <bits/stdc++.h> using namespace std; // returns true if there is triplet with sum equal? // to 'sum' present in A[]. Also, prints the triplet? bool find3Numbers(int A[], int arr_size, int sum)? {? ????int l, r;? ????// Fix the first element as A[i]? ????for (int i = 0; i < arr_size - 2; i++) ????{? ????????// Fix the second element as A[j]? ????????for (int j = i + 1; j < arr_size - 1; j++) ????????{? ????????????// Now look for the third number? ????????????for (int k = j + 1; k < arr_size; k++) ????????????{? ????????????????if (A[i] + A[j] + A[k] == sum) ????????????????{? ????????????????????cout << "Triplet is " << A[i] << ????????????????????????", " << A[j] << ", " << A[k];? ????????????????????return true;? ????????????????}? ????????????}? ????????}? ????}? ????// If we reach here, then no triplet was found? ????return false;? }? /* Driver code */ int main()? {? ????int A[] = { 1, 4, 45, 6, 10, 8 };? ????int sum = 22;? ????int arr_size = sizeof(A) / sizeof(A[0]);? ????find3Numbers(A, arr_size, sum);? ????return 0;? }? // This is code is contributed by rathbhupendra ``` ## C ``` #include <stdio.h> // returns true if there is triplet with sum equal // to 'sum' present in A[]. Also, prints the triplet bool find3Numbers(int A[], int arr_size, int sum) { ????int l, r; ????// Fix the first element as A[i] ????for (int i = 0; i < arr_size - 2; i++) { ????????// Fix the second element as A[j] ????????for (int j = i + 1; j < arr_size - 1; j++) { ????????????// Now look for the third number ????????????for (int k = j + 1; k < arr_size; k++) { ????????????????if (A[i] + A[j] + A[k] == sum) { ????????????????????printf("Triplet is %d, %d, %d", ???????????????????????????A[i], A[j], A[k]); ????????????????????return true; ????????????????} ????????????} ????????} ????} ????// If we reach here, then no triplet was found ????return false; } /* Driver program to test above function */ int main() { ????int A[] = { 1, 4, 45, 6, 10, 8 }; ????int sum = 22; ????int arr_size = sizeof(A) / sizeof(A[0]); ????find3Numbers(A, arr_size, sum); ????return 0; } ``` ## Java ``` // Java program to find a triplet class FindTriplet { ????// returns true if there is triplet with sum equal ????// to 'sum' present in A[]. Also, prints the triplet ????boolean find3Numbers(int A[], int arr_size, int sum) ????{ ????????int l, r; ????????// Fix the first element as A[i] ????????for (int i = 0; i < arr_size - 2; i++) { ????????????// Fix the second element as A[j] ????????????for (int j = i + 1; j < arr_size - 1; j++) { ????????????????// Now look for the third number ????????????????for (int k = j + 1; k < arr_size; k++) { ????????????????????if (A[i] + A[j] + A[k] == sum) { ????????????????????????System.out.print("Triplet is " + A[i] + ", " + A[j] + ", " + A[k]); ????????????????????????return true; ????????????????????} ????????????????} ????????????} ????????} ????????// If we reach here, then no triplet was found ????????return false; ????} ????// Driver program to test above functions ????public static void main(String[] args) ????{ ????????FindTriplet triplet = new FindTriplet(); ????????int A[] = { 1, 4, 45, 6, 10, 8 }; ????????int sum = 22; ????????int arr_size = A.length; ????????triplet.find3Numbers(A, arr_size, sum); ????} } ``` ## Python3 ``` # Python3 program to find a triplet? # that sum to a given value # returns true if there is triplet with # sum equal to 'sum' present in A[].? # Also, prints the triplet def find3Numbers(A, arr_size, sum): ????# Fix the first element as A[i] ????for i in range( 0, arr_size-2): ????????# Fix the second element as A[j] ????????for j in range(i + 1, arr_size-1):? ????????????# Now look for the third number ????????????for k in range(j + 1, arr_size): ????????????????if A[i] + A[j] + A[k] == sum: ????????????????????print("Triplet is", A[i], ??????????????????????????", ", A[j], ", ", A[k]) ????????????????????return True ????# If we reach here, then no? ????# triplet was found ????return False # Driver program to test above function? A = [1, 4, 45, 6, 10, 8] sum = 22 arr_size = len(A) find3Numbers(A, arr_size, sum) # This code is contributed by Smitha Dinesh Semwal? ``` ## C# ``` // C# program to find a triplet // that sum to a given value using System; class GFG { ????// returns true if there is ????// triplet with sum equal ????// to 'sum' present in A[]. ????// Also, prints the triplet ????static bool find3Numbers(int[] A, ?????????????????????????????int arr_size, ?????????????????????????????int sum) ????{ ????????// Fix the first ????????// element as A[i] ????????for (int i = 0; ?????????????i < arr_size - 2; i++) { ????????????// Fix the second ????????????// element as A[j] ????????????for (int j = i + 1; ?????????????????j < arr_size - 1; j++) { ????????????????// Now look for ????????????????// the third number ????????????????for (int k = j + 1; ?????????????????????k < arr_size; k++) { ????????????????????if (A[i] + A[j] + A[k] == sum) { ????????????????????????Console.WriteLine("Triplet is " + A[i] + ", " + A[j] + ", " + A[k]); ????????????????????????return true; ????????????????????} ????????????????} ????????????} ????????} ????????// If we reach here, ????????// then no triplet was found ????????return false; ????} ????// Driver Code ????static public void Main() ????{ ????????int[] A = { 1, 4, 45, 6, 10, 8 }; ????????int sum = 22; ????????int arr_size = A.Length; ????????find3Numbers(A, arr_size, sum); ????} } // This code is contributed by m_kit ``` ## PHP ``` <?php // PHP program to find a triplet? // that sum to a given value // returns true if there is? // triplet with sum equal to // 'sum' present in A[]. // Also, prints the triplet function find3Numbers($A, $arr_size, $sum) { ????$l; $r; ????// Fix the first ????// element as A[i] ????for ($i = 0;? ?????????$i < $arr_size - 2; $i++) ????{ ????// Fix the second? ????// element as A[j] ????for ($j = $i + 1;? ?????????$j < $arr_size - 1; $j++) ????{ ????????// Now look for the ????????// third number ????????for ($k = $j + 1;? ?????????????$k < $arr_size; $k++) ????????{ ????????????if ($A[$i] + $A[$j] +? ????????????????$A[$k] == $sum) ????????????{ ????????????????echo "Triplet is", " ", $A[$i], ??????????????????????????????????", ", $A[$j],? ??????????????????????????????????", ", $A[$k]; ????????????????return true; ????????????} ????????} ????} ????} ????// If we reach here, then ????// no triplet was found ????return false; } // Driver Code $A = array(1, 4, 45,? ???????????6, 10, 8); $sum = 22; $arr_size = sizeof($A); find3Numbers($A, $arr_size, $sum); // This code is contributed by ajit ?> ``` **輸出**: ``` Triplet is 4, 10, 8 ``` * **復雜度分析**: * **時間復雜度**: O(n <sup>3</sup> )。 數組中遍歷了三個嵌套循環,因此時間復雜度為 O(n ^ 3) * **空間復雜度**:`O(1)`。 由于不需要額外的空間。 **方法 2**: 此方法使用排序來提高代碼的效率。 * **方法**:通過對數組進行排序,可以提高算法的效率。 這種有效的方法使用了[兩指針技術](https://www.geeksforgeeks.org/given-an-array-a-and-a-number-x-check-for-pair-in-a-with-sum-as-x/)。 遍歷數組并修復三元組的第一個元素。 現在,使用“兩指針”算法查找是否存在一對總和等于 x – array [i]的對。 兩個指針算法需要線性時間,因此它比嵌套循環更好。 * **算法**: 1. 排序給定的數組。 2. 遍歷數組并修復可能的三元組的第一個元素 arr [i]。 3. 然后固定兩個指針,一個指向 i + 1,另一個指向 n-1。然后看一下總和, 1. 如果總和小于所需總和,則遞增第一個指針。 2. 否則,如果總和較大,請減小結束指針以減少總和。 3. 否則,如果兩個指針處的元素之和等于給定的和,則打印三元組并中斷。 * **實現**: ## C++ ``` // C++ program to find a triplet #include <bits/stdc++.h> using namespace std; // returns true if there is triplet with sum equal // to 'sum' present in A[]. Also, prints the triplet bool find3Numbers(int A[], int arr_size, int sum) { ????int l, r; ????/* Sort the elements */ ????sort(A, A + arr_size); ????/* Now fix the first element one by one and find the ???????other two elements */ ????for (int i = 0; i < arr_size - 2; i++) { ????????// To find the other two elements, start two index ????????// variables from two corners of the array and move ????????// them toward each other ????????l = i + 1; // index of the first element in the ????????// remaining elements ????????r = arr_size - 1; // index of the last element ????????while (l < r) { ????????????if (A[i] + A[l] + A[r] == sum) { ????????????????printf("Triplet is %d, %d, %d", A[i], ???????????????????????A[l], A[r]); ????????????????return true; ????????????} ????????????else if (A[i] + A[l] + A[r] < sum) ????????????????l++; ????????????else // A[i] + A[l] + A[r] > sum ????????????????r--; ????????} ????} ????// If we reach here, then no triplet was found ????return false; } /* Driver program to test above function */ int main() { ????int A[] = { 1, 4, 45, 6, 10, 8 }; ????int sum = 22; ????int arr_size = sizeof(A) / sizeof(A[0]); ????find3Numbers(A, arr_size, sum); ????return 0; } ``` ## Java ``` // Java program to find a triplet class FindTriplet { ????// returns true if there is triplet with sum equal ????// to 'sum' present in A[]. Also, prints the triplet ????boolean find3Numbers(int A[], int arr_size, int sum) ????{ ????????int l, r; ????????/* Sort the elements */ ????????quickSort(A, 0, arr_size - 1); ????????/* Now fix the first element one by one and find the ???????????other two elements */ ????????for (int i = 0; i < arr_size - 2; i++) { ????????????// To find the other two elements, start two index variables ????????????// from two corners of the array and move them toward each ????????????// other ????????????l = i + 1; // index of the first element in the remaining elements ????????????r = arr_size - 1; // index of the last element ????????????while (l < r) { ????????????????if (A[i] + A[l] + A[r] == sum) { ????????????????????System.out.print("Triplet is " + A[i] + ", " + A[l] + ", " + A[r]); ????????????????????return true; ????????????????} ????????????????else if (A[i] + A[l] + A[r] < sum) ????????????????????l++; ????????????????else // A[i] + A[l] + A[r] > sum ????????????????????r--; ????????????} ????????} ????????// If we reach here, then no triplet was found ????????return false; ????} ????int partition(int A[], int si, int ei) ????{ ????????int x = A[ei]; ????????int i = (si - 1); ????????int j; ????????for (j = si; j <= ei - 1; j++) { ????????????if (A[j] <= x) { ????????????????i++; ????????????????int temp = A[i]; ????????????????A[i] = A[j]; ????????????????A[j] = temp; ????????????} ????????} ????????int temp = A[i + 1]; ????????A[i + 1] = A[ei]; ????????A[ei] = temp; ????????return (i + 1); ????} ????/* Implementation of Quick Sort ????A[] --> Array to be sorted ????si? --> Starting index ????ei? --> Ending index ?????*/ ????void quickSort(int A[], int si, int ei) ????{ ????????int pi; ????????/* Partitioning index */ ????????if (si < ei) { ????????????pi = partition(A, si, ei); ????????????quickSort(A, si, pi - 1); ????????????quickSort(A, pi + 1, ei); ????????} ????} ????// Driver program to test above functions ????public static void main(String[] args) ????{ ????????FindTriplet triplet = new FindTriplet(); ????????int A[] = { 1, 4, 45, 6, 10, 8 }; ????????int sum = 22; ????????int arr_size = A.length; ????????triplet.find3Numbers(A, arr_size, sum); ????} } ``` ## Python3 ``` # Python3 program to find a triplet # returns true if there is triplet # with sum equal to 'sum' present # in A[]. Also, prints the triplet def find3Numbers(A, arr_size, sum): ????# Sort the elements? ????A.sort() ????# Now fix the first element? ????# one by one and find the ????# other two elements? ????for i in range(0, arr_size-2): ????????# To find the other two elements, ????????# start two index variables from ????????# two corners of the array and ????????# move them toward each other ????????# index of the first element ????????# in the remaining elements ????????l = i + 1? ????????# index of the last element ????????r = arr_size-1? ????????while (l < r): ????????????if( A[i] + A[l] + A[r] == sum): ????????????????print("Triplet is", A[i],? ?????????????????????', ', A[l], ', ', A[r]); ????????????????return True ????????????elif (A[i] + A[l] + A[r] < sum): ????????????????l += 1 ????????????else: # A[i] + A[l] + A[r] > sum ????????????????r -= 1 ????# If we reach here, then ????# no triplet was found ????return False # Driver program to test above function? A = [1, 4, 45, 6, 10, 8] sum = 22 arr_size = len(A) find3Numbers(A, arr_size, sum) # This is contributed by Smitha Dinesh Semwal ``` ## C# ``` // C# program to find a triplet using System; class GFG { ????// returns true if there is triplet ????// with sum equal to 'sum' present ????// in A[]. Also, prints the triplet ????bool find3Numbers(int[] A, int arr_size, ??????????????????????int sum) ????{ ????????int l, r; ????????/* Sort the elements */ ????????quickSort(A, 0, arr_size - 1); ????????/* Now fix the first element? ????one by one and find the ????other two elements */ ????????for (int i = 0; i < arr_size - 2; i++) { ????????????// To find the other two elements, ????????????// start two index variables from ????????????// two corners of the array and ????????????// move them toward each other ????????????l = i + 1; // index of the first element ????????????// in the remaining elements ????????????r = arr_size - 1; // index of the last element ????????????while (l < r) { ????????????????if (A[i] + A[l] + A[r] == sum) { ????????????????????Console.Write("Triplet is " + A[i] + ", " + A[l] + ", " + A[r]); ????????????????????return true; ????????????????} ????????????????else if (A[i] + A[l] + A[r] < sum) ????????????????????l++; ????????????????else // A[i] + A[l] + A[r] > sum ????????????????????r--; ????????????} ????????} ????????// If we reach here, then ????????// no triplet was found ????????return false; ????} ????int partition(int[] A, int si, int ei) ????{ ????????int x = A[ei]; ????????int i = (si - 1); ????????int j; ????????for (j = si; j <= ei - 1; j++) { ????????????if (A[j] <= x) { ????????????????i++; ????????????????int temp = A[i]; ????????????????A[i] = A[j]; ????????????????A[j] = temp; ????????????} ????????} ????????int temp1 = A[i + 1]; ????????A[i + 1] = A[ei]; ????????A[ei] = temp1; ????????return (i + 1); ????} ????/* Implementation of Quick Sort A[] --> Array to be sorted si --> Starting index ei --> Ending index */ ????void quickSort(int[] A, int si, int ei) ????{ ????????int pi; ????????/* Partitioning index */ ????????if (si < ei) { ????????????pi = partition(A, si, ei); ????????????quickSort(A, si, pi - 1); ????????????quickSort(A, pi + 1, ei); ????????} ????} ????// Driver Code ????static void Main() ????{ ????????GFG triplet = new GFG(); ????????int[] A = new int[] { 1, 4, 45, 6, 10, 8 }; ????????int sum = 22; ????????int arr_size = A.Length; ????????triplet.find3Numbers(A, arr_size, sum); ????} } // This code is contributed by mits ``` ## PHP ``` <?php // PHP program to find a triplet // returns true if there is? // triplet with sum equal to // 'sum' present in A[]. Also, // prints the triplet function find3Numbers($A, $arr_size, $sum) { ????$l; $r; ????/* Sort the elements */ ????sort($A); ????/* Now fix the first element? ????one by one and find the ????other two elements */ ????for ($i = 0; $i < $arr_size - 2; $i++)? ????{ ????????// To find the other two elements,? ????????// start two index variables from? ????????// two corners of the array and? ????????// move them toward each other ????????$l = $i + 1; // index of the first element? ?????????????????????// in the remaining elements ????????// index of the last element ????????$r = $arr_size - 1;? ????????while ($l < $r)? ????????{ ????????????if ($A[$i] + $A[$l] +? ????????????????$A[$r] == $sum) ????????????{ ????????????????echo "Triplet is ", $A[$i], " ", ????????????????????????????????????$A[$l], " ",? ????????????????????????????????????$A[$r], "\n"; ????????????????return true; ????????????} ????????????else if ($A[$i] + $A[$l] + ?????????????????????$A[$r] < $sum) ????????????????$l++; ????????????else // A[i] + A[l] + A[r] > sum ????????????????$r--; ????????} ????} ????// If we reach here, then ????// no triplet was found ????return false; } // Driver Code $A = array (1, 4, 45, 6, 10, 8); $sum = 22; $arr_size = sizeof($A); find3Numbers($A, $arr_size, $sum); // This code is contributed by ajit ?> ``` **輸出**: ``` Triplet is 4, 8, 10 ``` * **復雜度分析**: * **時間復雜度**: O(N ^ 2)。 只有兩個嵌套循環遍歷數組,因此時間復雜度為 O(n ^ 2)。 兩個指針算法需要`O(n)`時間,并且可以使用另一個嵌套遍歷來固定第一個元素。 * **空間復雜度**:`O(1)`。 由于不需要額外的空間。 **方法 3** :這是基于哈希的解決方案。 * **方法**:這種方法使用了額外的空間,但比兩個指針方法更簡單。 從頭到尾運行兩個循環,外循環,從 i + 1 到結束運行內循環。 創建一個哈希圖或設置為將元素存儲在 i + 1 到 j-1 之間。 因此,如果給定的總和為 x,請檢查集合中是否存在等于 x – arr [i] – arr [j]的數字。 如果是,請打印三元組。 * **算法**: 1. 從頭到尾遍歷數組。 (循環計數器 i) 2. 創建一個 HashMap 或設置為存儲唯一對。 3. 從 i + 1 到數組末尾運行另一個循環。 (循環計數器 j) 4. 如果集合中有一個元素等于 x- arr [i] – arr [j],則打印三元組(arr [i],arr [j],x-arr [i] -arr [j] )和打破 5. 將第 j 個元素插入集合中。 * **實現**: ## C++ ``` // C++ program to find a triplet using Hashing #include <bits/stdc++.h> using namespace std; // returns true if there is triplet with sum equal // to 'sum' present in A[]. Also, prints the triplet bool find3Numbers(int A[], int arr_size, int sum) { ????// Fix the first element as A[i] ????for (int i = 0; i < arr_size - 2; i++) { ????????// Find pair in subarray A[i+1..n-1] ????????// with sum equal to sum - A[i] ????????unordered_set<int> s; ????????int curr_sum = sum - A[i]; ????????for (int j = i + 1; j < arr_size; j++) { ????????????if (s.find(curr_sum - A[j]) != s.end()) { ????????????????printf("Triplet is %d, %d, %d", A[i], ???????????????????????A[j], curr_sum - A[j]); ????????????????return true; ????????????} ????????????s.insert(A[j]); ????????} ????} ????// If we reach here, then no triplet was found ????return false; } /* Driver program to test above function */ int main() { ????int A[] = { 1, 4, 45, 6, 10, 8 }; ????int sum = 22; ????int arr_size = sizeof(A) / sizeof(A[0]); ????find3Numbers(A, arr_size, sum); ????return 0; } ``` ## Java ``` // Java program to find a triplet using Hashing import java.util.*; class GFG { ????// returns true if there is triplet ????// with sum equal to 'sum' present ????// in A[]. Also, prints the triplet ????static boolean find3Numbers(int A[], ????????????????????????????????int arr_size, int sum) ????{ ????????// Fix the first element as A[i] ????????for (int i = 0; i < arr_size - 2; i++) { ????????????// Find pair in subarray A[i+1..n-1] ????????????// with sum equal to sum - A[i] ????????????HashSet<Integer> s = new HashSet<Integer>(); ????????????int curr_sum = sum - A[i]; ????????????for (int j = i + 1; j < arr_size; j++) { ????????????????if (s.contains(curr_sum - A[j])) { ????????????????????System.out.printf("Triplet is %d, %d, %d", A[i], ??????????????????????????????????????A[j], curr_sum - A[j]); ????????????????????return true; ????????????????} ????????????????s.add(A[j]); ????????????} ????????} ????????// If we reach here, then no triplet was found ????????return false; ????} ????/* Driver code */ ????public static void main(String[] args) ????{ ????????int A[] = { 1, 4, 45, 6, 10, 8 }; ????????int sum = 22; ????????int arr_size = A.length; ????????find3Numbers(A, arr_size, sum); ????} } // This code has been contributed by 29AjayKumar ``` ## Python3 ``` # Python3 program to find a triplet using Hashing # returns true if there is triplet with sum equal # to 'sum' present in A[]. Also, prints the triplet def find3Numbers(A, arr_size, sum): ????for i in range(0, arr_size-1): ????????# Find pair in subarray A[i + 1..n-1]? ????????# with sum equal to sum - A[i] ????????s = set() ????????curr_sum = sum - A[i] ????????for j in range(i + 1, arr_size): ????????????if (curr_sum - A[j]) in s: ????????????????print("Triplet is", A[i],? ????????????????????????", ", A[j], ", ", curr_sum-A[j]) ????????????????return True ????????????s.add(A[j]) ????return False # Driver program to test above function? A = [1, 4, 45, 6, 10, 8]? sum = 22 arr_size = len(A)? find3Numbers(A, arr_size, sum)? # This is contributed by Yatin gupta ``` ## C# ``` // C# program to find a triplet using Hashing using System; using System.Collections.Generic; public class GFG { ????// returns true if there is triplet ????// with sum equal to 'sum' present ????// in A[]. Also, prints the triplet ????static bool find3Numbers(int[] A, ?????????????????????????????int arr_size, int sum) ????{ ????????// Fix the first element as A[i] ????????for (int i = 0; i < arr_size - 2; i++) { ????????????// Find pair in subarray A[i+1..n-1] ????????????// with sum equal to sum - A[i] ????????????HashSet<int> s = new HashSet<int>(); ????????????int curr_sum = sum - A[i]; ????????????for (int j = i + 1; j < arr_size; j++) { ????????????????if (s.Contains(curr_sum - A[j])) { ????????????????????Console.Write("Triplet is {0}, {1}, {2}", A[i], ??????????????????????????????????A[j], curr_sum - A[j]); ????????????????????return true; ????????????????} ????????????????s.Add(A[j]); ????????????} ????????} ????????// If we reach here, then no triplet was found ????????return false; ????} ????/* Driver code */ ????public static void Main() ????{ ????????int[] A = { 1, 4, 45, 6, 10, 8 }; ????????int sum = 22; ????????int arr_size = A.Length; ????????find3Numbers(A, arr_size, sum); ????} } /* This code contributed by PrinciRaj1992 */ ``` **輸出**: ``` Triplet is 4, 8, 10 ``` * **復雜度分析**: * **時間復雜度**: O(N ^ 2)。 只有兩個嵌套循環遍歷數組,因此時間復雜度為 O(n ^ 2)。 * **空間復雜度**:`O(1)`。 由于不需要額外的空間。 **如何打印給定總和的所有三胞胎?** 請參考[查找所有零和的三元組](https://www.geeksforgeeks.org/find-triplets-array-whose-sum-equal-zero/)
                  <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>

                              哎呀哎呀视频在线观看