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

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # 找到所有零和的三元組 > 原文: [https://www.geeksforgeeks.org/find-triplets-array-whose-sum-equal-zero/](https://www.geeksforgeeks.org/find-triplets-array-whose-sum-equal-zero/) 給定一系列不同的元素。 任務是在總和為零的數組中查找三元組。 **示例**: ``` Input : arr[] = {0, -1, 2, -3, 1} Output : (0 -1 1), (2 -3 1) Explanation : The triplets with zero sum are 0 + -1 + 1 = 0 and 2 + -3 + 1 = 0 Input : arr[] = {1, -2, 1, 0, 5} Output : 1 -2 1 Explanation : The triplets with zero sum is 1 + -2 + 1 = 0 ``` **方法 1**: 這是一種簡單的方法,需要 O(n <sup>3</sup> )時間才能得出結果。 * **方法**:樸素的方法運行三個循環,并一一檢查三個元素的總和是否為零。 如果三個元素的總和為零,則打印元素,否則找不到打印。 * **算法**: 1. 使用循環計數器 *i* , *j* , *k* 運行三個嵌套循環 2. 這三個循環從 0 到 n-3,第二個循環從 i + 1 到 n-2,第三個循環從 j + 1 到 n-1。 循環計數器代表三元組的三個元素。 3. 檢查第 i,第 j,第 k 個元素的總和是否等于零。 如果是,則打印總和,否則繼續。 * **實現**: ## C++ ``` // A simple C++ program to find three elements // whose sum is equal to zero #include<bits/stdc++.h> using namespace std; // Prints all triplets in arr[] with 0 sum void findTriplets(int arr[], int n) { ????bool found = true; ????for (int i=0; i<n-2; i++) ????{ ????????for (int j=i+1; j<n-1; j++) ????????{ ????????????for (int k=j+1; k<n; k++) ????????????{ ????????????????if (arr[i]+arr[j]+arr[k] == 0) ????????????????{ ????????????????????cout << arr[i] << " " ?????????????????????????<< arr[j] << " " ?????????????????????????<< arr[k] <<endl; ????????????????????found = true; ????????????????} ????????????} ????????} ????} ????// If no triplet with 0 sum found in array ????if (found == false) ????????cout << " not exist "<<endl; } // Driver code int main() { ????int arr[] = {0, -1, 2, -3, 1}; ????int n = sizeof(arr)/sizeof(arr[0]); ????findTriplets(arr, n); ????return 0; } ``` ## Java ``` // A simple Java program to find three elements // whose sum is equal to zero class num{ // Prints all triplets in arr[] with 0 sum static void findTriplets(int[] arr, int n) { ????boolean found = true; ????for (int i=0; i<n-2; i++) ????{ ????????for (int j=i+1; j<n-1; j++) ????????{ ????????????for (int k=j+1; k<n; k++) ????????????{ ????????????????if (arr[i]+arr[j]+arr[k] == 0) ????????????????{ ????????????????????System.out.print(arr[i]); ????????????????????System.out.print(" "); ????????????????????System.out.print(arr[j]); ????????????????????System.out.print(" "); ????????????????????System.out.print(arr[k]); ????????????????????System.out.print("\n"); ????????????????????found = true; ????????????????} ????????????} ????????} ????} ????// If no triplet with 0 sum found in array ????if (found == false) ????????System.out.println(" not exist "); } // Driver code public static void main(String[] args) { ????int arr[] = {0, -1, 2, -3, 1}; ????int n =arr.length; ????findTriplets(arr, n); } } //This code is contributed by //Smitha Dinesh Semwal ``` ## Python3 ``` # A simple Python 3 program? # to find three elements whose? # sum is equal to zero # Prints all triplets in? # arr[] with 0 sum def findTriplets(arr, n): ????found = True ????for i in range(0, n-2): ????????for j in range(i+1, n-1): ????????????for k in range(j+1, n): ????????????????if (arr[i] + arr[j] + arr[k] == 0): ????????????????????print(arr[i], arr[j], arr[k]) ????????????????????found = True ????# If no triplet with 0 sum? ????# found in array ????if (found == False): ????????print(" not exist ") # Driver code arr = [0, -1, 2, -3, 1] n = len(arr) findTriplets(arr, n) # This code is contributed by Smitha Dinesh Semwal???? ``` ## C# ``` // A simple C# program to find three elements? // whose sum is equal to zero using System; class GFG { ????// Prints all triplets in arr[] with 0 sum ????static void findTriplets(int []arr, int n) ????{ ????????bool found = true; ????????for (int i = 0; i < n-2; i++) ????????{ ????????????for (int j = i+1; j < n-1; j++) ????????????{ ????????????????for (int k = j+1; k < n; k++) ????????????????{ ????????????????????if (arr[i] + arr[j] + arr[k] ???????????????????????????????????????????== 0) ????????????????????{ ????????????????????????Console.Write(arr[i]); ????????????????????????Console.Write(" "); ????????????????????????Console.Write(arr[j]); ????????????????????????Console.Write(" "); ????????????????????????Console.Write(arr[k]); ????????????????????????Console.Write("\n"); ????????????????????????found = true; ????????????????????} ????????????????} ????????????} ????????} ????????// If no triplet with 0 sum found in? ????????// array ????????if (found == false) ????????????Console.Write(" not exist "); ????} ????// Driver code ????public static void Main() ????{ ????????int []arr = {0, -1, 2, -3, 1}; ????????int n = arr.Length; ????????findTriplets(arr, n); ????} } // This code is contributed by nitin mittal. ``` ## PHP ``` <?php // A simple PHP program to? // find three elements whose? // sum is equal to zero // Prints all triplets // in arr[] with 0 sum function findTriplets($arr, $n) { ????$found = true; ????for ($i = 0; $i < $n - 2; $i++) ????{ ????????for ($j = $i + 1; $j < $n - 1; $j++) ????????{ ????????????for ($k = $j + 1; $k < $n; $k++) ????????????{ ????????????????if ($arr[$i] + $arr[$j] +? ???????????????????????????????$arr[$k] == 0) ????????????????{ ????????????????????echo $arr[$i] , " ", ?????????????????????????$arr[$j] , " ", ?????????????????????????$arr[$k] ,"\n"; ????????????????????$found = true; ????????????????} ????????????} ????????} ????} ????// If no triplet with 0 ????// sum found in array ????if ($found == false) ????????echo " not exist ", "\n"; } // Driver Code $arr = array (0, -1, 2, -3, 1); $n = sizeof($arr); findTriplets($arr, $n); // This code is contributed by m_kit ?> ``` **輸出**: ``` 0 -1 1 2 -3 1 ``` * **復雜度分析**: * **時間復雜度**: O(n <sup>3</sup> )。 由于需要三個嵌套循環,因此時間復雜度為 O(n <sup>3</sup> )。 * **輔助空間**:`O(1)`。 由于不需要額外的空間,因此時間復雜度是恒定的。 **方法 2**: 第二種方法使用哈希處理來獲得結果,并在較短的 `O(n^2)`上求解。 * **方法**:這涉及遍歷數組。 對于每個元素 arr [i],找到一個總和為“ -arr [i]”的對。 這個問題簡化為成對和,可以使用哈希在`O(n)`時間內解決。 * **算法**: 1. 創建一個哈希來存儲鍵值對。 2. 運行帶有兩個循環的嵌套循環,外循環從 0 到 n-2,內循環從 i + 1 到 n-1 3. 檢查哈希圖中是否存在 ith 和 jth 元素的總和乘以-1 4. 如果該元素存在于哈希圖中,則打印三元組,否則將第 j 個元素插入哈希圖中。 * **實現**: ## C++ ``` // C++ program to find triplets in a given // array whose sum is zero #include<bits/stdc++.h> using namespace std; // function to print triplets with 0 sum void findTriplets(int arr[], int n) { ????bool found = false; ????for (int i=0; i<n-1; i++) ????{ ????????// Find all pairs with sum equals to ????????// "-arr[i]" ????????unordered_set<int> s; ????????for (int j=i+1; j<n; j++) ????????{ ????????????int x = -(arr[i] + arr[j]); ????????????if (s.find(x) != s.end()) ????????????{ ????????????????printf("%d %d %d\n", x, arr[i], arr[j]); ????????????????found = true; ????????????} ????????????else ????????????????s.insert(arr[j]); ????????} ????} ????if (found == false) ????????cout << " No Triplet Found" << endl; } // Driver code int main() { ????int arr[] = {0, -1, 2, -3, 1}; ????int n = sizeof(arr)/sizeof(arr[0]); ????findTriplets(arr, n); ????return 0; } ``` ## Java ``` // Java program to find triplets in a given // array whose sum is zero import java.util.*; class GFG? { ????// function to print triplets with 0 sum ????static void findTriplets(int arr[], int n)? ????{ ????????boolean found = false; ????????for (int i = 0; i < n - 1; i++)? ????????{ ????????????// Find all pairs with sum equals to ????????????// "-arr[i]" ????????????HashSet<Integer> s = new HashSet<Integer>(); ????????????for (int j = i + 1; j < n; j++)? ????????????{ ????????????????int x = -(arr[i] + arr[j]); ????????????????if (s.contains(x))? ????????????????{ ????????????????????System.out.printf("%d %d %d\n", x, arr[i], arr[j]); ????????????????????found = true; ????????????????}? ????????????????else? ????????????????{ ????????????????????s.add(arr[j]); ????????????????} ????????????} ????????} ????????if (found == false) ????????{ ????????????System.out.printf(" No Triplet Found\n"); ????????} ????} ????// Driver code ????public static void main(String[] args)? ????{ ????????int arr[] = {0, -1, 2, -3, 1}; ????????int n = arr.length; ????????findTriplets(arr, n); ????} } // This code contributed by Rajput-Ji ``` ## Python3 ``` # Python3 program to find triplets? # in a given array whose sum is zero? # function to print triplets with 0 sum? def findTriplets(arr, n): ????found = False ????for i in range(n - 1): ????????# Find all pairs with sum? ????????# equals to "-arr[i]"? ????????s = set() ????????for j in range(i + 1, n): ????????????x = -(arr[i] + arr[j]) ????????????if x in s: ????????????????print(x, arr[i], arr[j]) ????????????????found = True ????????????else: ????????????????s.add(arr[j]) ????if found == False: ????????print("No Triplet Found") # Driver Code arr = [0, -1, 2, -3, 1] n = len(arr) findTriplets(arr, n) # This code is contributed by Shrikant13 ``` ## C# ``` // C# program to find triplets in a given // array whose sum is zero using System; using System.Collections.Generic; class GFG? { ????// function to print triplets with 0 sum ????static void findTriplets(int []arr, int n)? ????{ ????????bool found = false; ????????for (int i = 0; i < n - 1; i++)? ????????{ ????????????// Find all pairs with sum equals to ????????????// "-arr[i]" ????????????HashSet<int> s = new HashSet<int>(); ????????????for (int j = i + 1; j < n; j++)? ????????????{ ????????????????int x = -(arr[i] + arr[j]); ????????????????if (s.Contains(x))? ????????????????{ ????????????????????Console.Write("{0} {1} {2}\n", x, arr[i], arr[j]); ????????????????????found = true; ????????????????}? ????????????????else ????????????????{ ????????????????????s.Add(arr[j]); ????????????????} ????????????} ????????} ????????if (found == false) ????????{ ????????????Console.Write(" No Triplet Found\n"); ????????} ????} ????// Driver code ????public static void Main(String[] args)? ????{ ????????int []arr = {0, -1, 2, -3, 1}; ????????int n = arr.Length; ????????findTriplets(arr, n); ????} } // This code has been contributed by 29AjayKumar ``` **輸出**: ``` -1 0 1 -3 2 1 ``` * **復雜度分析**: * **時間復雜度**: `O(n^2)`。 由于需要兩個嵌套循環,因此時間復雜度為 `O(n^2)`。 * **輔助空間**:`O(n)`。 由于需要一個哈希圖,因此時間復雜度是線性的。 **方法 3**: 該方法使用排序來得出正確的結果,并以 `O(n^2)`的時間求解。 * **方法**:上述方法需要額外的空間。 該想法基于此帖子的[的方法 2。 對于每個元素,檢查是否存在一對總和等于該元素的負值的對。](https://www.geeksforgeeks.org/find-a-triplet-that-sum-to-a-given-value/) * **算法**: 1. 以升序對數組進行排序。 2. 從頭到尾遍歷數組。 3. 對于每個索引 *i* ,創建兩個變量 *l = i + 1* 和 *r = n – 1* 4. 循環運行直到 l 小于 r,如果 array [l],array [r]的總和等于零,則打印三元組并中斷循環 5. 如果總和小于零,則增加 l 的值,通過增加 l 的值,總和將隨著數組的排序而增加,因此 *array [l + 1] > array [l]* 6. 如果總和大于零,則 r 的值遞減,通過增加 l 的值,總和將隨著對數組進行排序而減小,因此 *array [r-1] < array [r]* 。 * **實現**: ## C++ ``` // C++ program to find triplets in a given // array whose sum is zero #include<bits/stdc++.h> using namespace std; // function to print triplets with 0 sum void findTriplets(int arr[], int n) { ????bool found = false; ????// sort array elements ????sort(arr, arr+n); ????for (int i=0; i<n-1; i++) ????{ ????????// initialize left and right ????????int l = i + 1; ????????int r = n - 1; ????????int x = arr[i]; ????????while (l < r) ????????{ ????????????if (x + arr[l] + arr[r] == 0) ????????????{ ????????????????// print elements if it's sum is zero ????????????????printf("%d %d %d\n", x, arr[l], arr[r]); ????????????????l++; ????????????????r--; ????????????????found = true; ????????????} ????????????// If sum of three elements is less ????????????// than zero then increment in left ????????????else if (x + arr[l] + arr[r] < 0) ????????????????l++; ????????????// if sum is greater than zero than ????????????// decrement in right side ????????????else ????????????????r--; ????????} ????} ????if (found == false) ????????cout << " No Triplet Found" << endl; } // Driven source int main() { ????int arr[] = {0, -1, 2, -3, 1}; ????int n = sizeof(arr)/sizeof(arr[0]); ????findTriplets(arr, n); ????return 0; } ``` ## Java ``` // Java? program to find triplets in a given // array whose sum is zero import java.util.Arrays;? import java.io.*; class GFG { ????// function to print triplets with 0 sum static void findTriplets(int arr[], int n) { ????boolean found = false; ????// sort array elements ????Arrays.sort(arr); ????for (int i=0; i<n-1; i++) ????{ ????????// initialize left and right ????????int l = i + 1; ????????int r = n - 1; ????????int x = arr[i]; ????????while (l < r) ????????{ ????????????if (x + arr[l] + arr[r] == 0) ????????????{ ????????????????// print elements if it's sum is zero ????????????????????System.out.print(x + " "); ????????????????????System.out.print(arr[l]+ " "); ????????????????????System.out.println(arr[r]+ " "); ????????????????l++; ????????????????r--; ????????????????found = true; ????????????} ????????????// If sum of three elements is less ????????????// than zero then increment in left ????????????else if (x + arr[l] + arr[r] < 0) ????????????????l++; ????????????// if sum is greater than zero than ????????????// decrement in right side ????????????else ????????????????r--; ????????} ????} ????if (found == false) ????????????System.out.println(" No Triplet Found"); } // Driven source ????public static void main (String[] args) { ????int arr[] = {0, -1, 2, -3, 1}; ????int n =arr.length; ????findTriplets(arr, n); ????} //This code is contributed by Tushil..???? } ``` ## Python3 ``` # python program to find triplets in a given # array whose sum is zero # function to print triplets with 0 sum def findTriplets(arr, n): ????found = False ????# sort array elements ????arr.sort() ????for i in range(0, n-1): ????????# initialize left and right ????????l = i + 1 ????????r = n - 1 ????????x = arr[i] ????????while (l < r): ????????????if (x + arr[l] + arr[r] == 0): ????????????????# print elements if it's sum is zero ????????????????print(x, arr[l], arr[r]) ????????????????l+=1 ????????????????r-=1 ????????????????found = True ????????????# If sum of three elements is less ????????????# than zero then increment in left ????????????elif (x + arr[l] + arr[r] < 0): ????????????????l+=1 ????????????# if sum is greater than zero than ????????????# decrement in right side ????????????else: ????????????????r-=1 ????if (found == False): ????????print(" No Triplet Found") # Driven source arr = [0, -1, 2, -3, 1] n = len(arr) findTriplets(arr, n) # This code is contributed by Smitha Dinesh Semwal ``` ## C# ``` // C#? program to find triplets in a given // array whose sum is zero using System; public class GFG{ ????????// function to print triplets with 0 sum static void findTriplets(int []arr, int n) { ????bool found = false; ????// sort array elements ????Array.Sort(arr); ????for (int i=0; i<n-1; i++) ????{ ????????// initialize left and right ????????int l = i + 1; ????????int r = n - 1; ????????int x = arr[i]; ????????while (l < r) ????????{ ????????????if (x + arr[l] + arr[r] == 0) ????????????{ ????????????????// print elements if it's sum is zero ????????????????????Console.Write(x + " "); ????????????????????Console.Write(arr[l]+ " "); ????????????????????Console.WriteLine(arr[r]+ " "); ????????????????l++; ????????????????r--; ????????????????found = true; ????????????} ????????????// If sum of three elements is less ????????????// than zero then increment in left ????????????else if (x + arr[l] + arr[r] < 0) ????????????????l++; ????????????// if sum is greater than zero than ????????????// decrement in right side ????????????else ????????????????r--; ????????} ????} ????if (found == false) ????????????Console.WriteLine(" No Triplet Found"); } // Driven source ????static public void Main (){ ????int []arr = {0, -1, 2, -3, 1}; ????int n =arr.Length; ????findTriplets(arr, n); ????} //This code is contributed by akt_mit..? } ``` ## PHP ``` <?php // PHP program to find? // triplets in a given // array whose sum is zero // function to print? // triplets with 0 sum function findTriplets($arr, $n) { ????$found = false; ????// sort array elements ????sort($arr); ????for ($i = 0; $i < $n - 1; $i++) ????{ ????????// initialize left ????????// and right ????????$l = $i + 1; ????????$r = $n - 1; ????????$x = $arr[$i]; ????????while ($l < $r) ????????{ ????????????if ($x + $arr[$l] +? ?????????????????????$arr[$r] == 0) ????????????{ ????????????????// print elements if? ????????????????// it's sum is zero ????????????????echo $x," ", $arr[$l], ????????????????????????" ", $arr[$r], "\n"; ????????????????$l++; ????????????????$r--; ????????????????$found = true; ????????????} ????????????// If sum of three elements? ????????????// is less than zero then? ????????????// increment in left ????????????else if ($x + $arr[$l] +? ??????????????????????????$arr[$r] < 0) ????????????????$l++; ????????????// if sum is greater than? ????????????// zero than decrement ????????????// in right side ????????????else ????????????????$r--; ????????} ????} ????if ($found == false) ????????echo " No Triplet Found" ,"\n"; } // Driver Code $arr = array (0, -1, 2, -3, 1); $n = sizeof($arr); findTriplets($arr, $n); // This code is contributed by ajit ?> ``` **輸出**: ``` -3 1 2 -1 0 1 ``` * **復雜度分析**: * **時間復雜度**: `O(n^2)`。 僅需要兩個嵌套循環,因此時間復雜度為 `O(n^2)`。 * **輔助空間**:`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>

                              哎呀哎呀视频在线观看