<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國際加速解決方案。 廣告
                # 查找給定總和的子數組| 系列 1(負數) > 原文: [https://www.geeksforgeeks.org/find-subarray-with-given-sum/](https://www.geeksforgeeks.org/find-subarray-with-given-sum/) 給定一個非排序的非負整數數組,找到一個連續的子數組,該數組加到給定的數字上。 **示例**: ``` Input: arr[] = {1, 4, 20, 3, 10, 5}, sum = 33 Ouptut: Sum found between indexes 2 and 4 Sum of elements between indices 2 and 4 is 20 + 3 + 10 = 33 Input: arr[] = {1, 4, 0, 0, 3, 10, 5}, sum = 7 Ouptut: Sum found between indexes 1 and 4 Sum of elements between indices 1 and 4 is 4 + 0 + 0 + 3 = 7 Input: arr[] = {1, 4}, sum = 0 Output: No subarray found There is no subarray with 0 sum ``` 可能有多個子數組,且 sum 為給定的 sum。 以下解決方案將首先打印此類子數組。 **簡單方法**: 一個簡單的解決方案是一個一個地考慮所有子數組并檢查每個子數組的總和。 以下程序實現簡單的解決方案。 運行兩個循環:外部循環選擇起點 I,內部循環嘗試從 i 開始的所有子數組。 **算法**: 1. 從頭到尾遍歷數組。 2. 從每個索引開始,從 *i* 到數組末尾的另一個循環將使所有子數組從 i 開始,并保持一個變量總和以計算總和。 3. 對于內循環更新中的每個索引 *sum = sum + array [j]* 4. 如果總和等于給定總和,則打印子數組。 ## C++ ```cpp /* A simple program to print subarray? with sum as given sum */ #include <bits/stdc++.h> using namespace std; /* Returns true if the there is a subarray? of arr[] with sum equal to 'sum' otherwise? returns false. Also, prints the result */ int subArraySum(int arr[], int n, int sum) { ????int curr_sum, i, j; ????// Pick a starting point ????for (i = 0; i < n; i++) { ????????curr_sum = arr[i]; ????????// try all subarrays starting with 'i' ????????for (j = i + 1; j <= n; j++) { ????????????if (curr_sum == sum) { ????????????????cout << "Sum found between indexes " ?????????????????????<< i << " and " << j - 1; ????????????????return 1; ????????????} ????????????if (curr_sum > sum || j == n) ????????????????break; ????????????curr_sum = curr_sum + arr[j]; ????????} ????} ????cout << "No subarray found"; ????return 0; } // Driver Code int main() { ????int arr[] = { 15, 2, 4, 8, 9, 5, 10, 23 }; ????int n = sizeof(arr) / sizeof(arr[0]); ????int sum = 23; ????subArraySum(arr, n, sum); ????return 0; } // This code is contributed // by rathbhupendra ``` ## C ``` /* A simple program to print? subarray with sum as given sum */ #include <stdio.h> /* Returns true if the there is a subarray? of arr[] with a sum equal to 'sum' ???otherwise returns false.? Also, prints? the result */ int subArraySum(int arr[], int n, int sum) { ????int curr_sum, i, j; ????// Pick a starting point ????for (i = 0; i < n; i++) { ????????curr_sum = arr[i]; ????????// try all subarrays starting with 'i' ????????for (j = i + 1; j <= n; j++) { ????????????if (curr_sum == sum) { ????????????????printf( ????????????????????"Sum found between indexes %d and %d", ????????????????????i, j - 1); ????????????????return 1; ????????????} ????????????if (curr_sum > sum || j == n) ????????????????break; ????????????curr_sum = curr_sum + arr[j]; ????????} ????} ????printf("No subarray found"); ????return 0; } // Driver program to test above function int main() { ????int arr[] = { 15, 2, 4, 8, 9, 5, 10, 23 }; ????int n = sizeof(arr) / sizeof(arr[0]); ????int sum = 23; ????subArraySum(arr, n, sum); ????return 0; } ``` ## Java ```java class SubarraySum { ????/* Returns true if the there is a? subarray of arr[] with a sum equal to ???????'sum' otherwise returns false.?? Also, prints the result */ ????int subArraySum(int arr[], int n, int sum) ????{ ????????int curr_sum, i, j; ????????// Pick a starting point ????????for (i = 0; i < n; i++) { ????????????curr_sum = arr[i]; ????????????// try all subarrays starting with 'i' ????????????for (j = i + 1; j <= n; j++) { ????????????????if (curr_sum == sum) { ????????????????????int p = j - 1; ????????????????????System.out.println( ????????????????????????"Sum found between indexes " + i ????????????????????????+ " and " + p); ????????????????????return 1; ????????????????} ????????????????if (curr_sum > sum || j == n) ????????????????????break; ????????????????curr_sum = curr_sum + arr[j]; ????????????} ????????} ????????System.out.println("No subarray found"); ????????return 0; ????} ????public static void main(String[] args) ????{ ????????SubarraySum arraysum = new SubarraySum(); ????????int arr[] = { 15, 2, 4, 8, 9, 5, 10, 23 }; ????????int n = arr.length; ????????int sum = 23; ????????arraysum.subArraySum(arr, n, sum); ????} } // This code has been contributed by Mayank Jaiswal(mayank_24) ``` ## Python3 ```py # Returns true if the # there is a subarray # of arr[] with sum # equal to 'sum'? # otherwise returns # false. Also, prints # the result? def subArraySum(arr, n, sum): ????# Pick a starting? ????# point ????for i in range(n): ????????curr_sum = arr[i] ????????# try all subarrays ????????# starting with 'i' ????????j = i + 1 ????????while j <= n: ????????????if curr_sum == sum: ????????????????print ("Sum found between") ????????????????print("indexes % d and % d"%( i, j-1)) ????????????????return 1 ????????????if curr_sum > sum or j == n: ????????????????break ????????????curr_sum = curr_sum + arr[j] ????????????j += 1 ????print ("No subarray found") ????return 0 # Driver program? arr = [15, 2, 4, 8, 9, 5, 10, 23] n = len(arr) sum = 23 subArraySum(arr, n, sum) # This code is Contributed by shreyanshi_arun. ``` ## C# ```cs // C# code to Find subarray // with given sum using System; class GFG { ????// Returns true if the there is a ????// subarray of arr[] with sum ????// equal to 'sum' otherwise returns ????// false. Also, prints the result ????int subArraySum(int[] arr, int n, ????????????????????int sum) ????{ ????????int curr_sum, i, j; ????????// Pick a starting point ????????for (i = 0; i < n; i++) { ????????????curr_sum = arr[i]; ????????????// try all subarrays ????????????// starting with 'i' ????????????for (j = i + 1; j <= n; j++) { ????????????????if (curr_sum == sum) { ????????????????????int p = j - 1; ????????????????????Console.Write("Sum found between " ??????????????????????????????????+ "indexes " + i + " and " + p); ????????????????????return 1; ????????????????} ????????????????if (curr_sum > sum || j == n) ????????????????????break; ????????????????curr_sum = curr_sum + arr[j]; ????????????} ????????} ????????Console.Write("No subarray found"); ????????return 0; ????} ????// Driver Code ????public static void Main() ????{ ????????GFG arraysum = new GFG(); ????????int[] arr = { 15, 2, 4, 8, 9, 5, 10, 23 }; ????????int n = arr.Length; ????????int sum = 23; ????????arraysum.subArraySum(arr, n, sum); ????} } // This code has been contributed // by nitin mittal ``` ## PHP ```php <?php // A simple program to print subarray // with sum as given sum? /* Returns true if the there is ???a subarray of arr[] with? ???sum equal to 'sum' ???otherwise returns false.? ???Also, prints the result */ function subArraySum($arr, $n, $sum) { ????$curr_sum; $i; $j; ????// Pick a starting point ????for ($i = 0; $i < $n; $i++) ????{ ????????$curr_sum = $arr[$i]; ????????// try all subarrays? ????????// starting with 'i' ????????for ($j = $i + 1; $j <= $n; $j++) ????????{ ????????????if ($curr_sum == $sum) ????????????{ ????????????????echo "Sum found between indexes ", $i, " and ", $j-1 ; ????????????????return 1; ????????????} ????????????if ($curr_sum > $sum || $j == $n) ????????????????break; ????????$curr_sum = $curr_sum + $arr[$j]; ????????} ????} ????echo "No subarray found"; ????return 0; } ????// Driver Code ????$arr= array(15, 2, 4, 8, 9, 5, 10, 23); ????$n = sizeof($arr); ????$sum = 23; ????subArraySum($arr, $n, $sum); ????return 0; // This code is contributed by AJit ?> ``` **輸出**: ``` Sum found between indexes 1 and 4 ``` **復雜度分析**: * **時間復雜度:最壞情況下的** O(n ^ 2)。 嵌套循環用于遍歷數組,因此時間復雜度為 O(n ^ 2) * **空間復雜度**:`O(1)`。 由于需要恒定的額外空間。 **有效方法**: 有一個想法,即數組的所有元素都是正數。 如果子數組的總和大于給定的總和,則不可能將元素添加到當前子數組中,總和為 *x* (給定總和)。 想法是對滑動窗口使用類似的方法。 從一個空的子數組開始,向該子數組添加元素,直到總和小于 *x* 為止。 如果總和大于 *x* ,則從當前子數組的開頭刪除元素。 **Algorithm:** 1. 創建三個變量 *l = 0,總和= 0* 2. 從頭到尾遍歷數組。 3. 通過添加當前元素來更新變量 sum, *sum = sum + array [i]* 4. 如果總和大于給定總和,則將變量總和更新為 *sum = sum – array [l]* ,并將 l 更新為 l ++。 5. 如果總和等于給定總和,則打印子數組并中斷循環。 ## C++ ``` /* An efficient program to print? subarray with sum as given sum */ #include <iostream> using namespace std; /* Returns true if the there is a subarray of? arr[] with a sum equal to 'sum' otherwise? returns false. Also, prints the result */ int subArraySum(int arr[], int n, int sum) { ????/* Initialize curr_sum as value of? ????first element and starting point as 0 */ ????int curr_sum = arr[0], start = 0, i; ????/* Add elements one by one to curr_sum and? ????if the curr_sum exceeds the sum, ????then remove starting element */ ????for (i = 1; i <= n; i++) { ????????// If curr_sum exceeds the sum, ????????// then remove the starting elements ????????while (curr_sum > sum && start < i - 1) { ????????????curr_sum = curr_sum - arr[start]; ????????????start++; ????????} ????????// If curr_sum becomes equal to sum, ????????// then return true ????????if (curr_sum == sum) { ????????????cout << "Sum found between indexes " ?????????????????<< start << " and " << i - 1; ????????????return 1; ????????} ????????// Add this element to curr_sum ????????if (i < n) ????????????curr_sum = curr_sum + arr[i]; ????} ????// If we reach here, then no subarray ????cout << "No subarray found"; ????return 0; } // Driver Code int main() { ????int arr[] = { 15, 2, 4, 8, 9, 5, 10, 23 }; ????int n = sizeof(arr) / sizeof(arr[0]); ????int sum = 23; ????subArraySum(arr, n, sum); ????return 0; } // This code is contributed by SHUBHAMSINGH10 ``` ## C ``` /* An efficient program to print? subarray with sum as given sum */ #include <stdio.h> /* Returns true if the there is a? subarray of arr[] with a sum? equal to 'sum' otherwise returns? false.? Also, prints the result */ int subArraySum(int arr[], int n, int sum) { ????/* Initialize curr_sum as? ???????value of first element and? starting point as 0 */ ????int curr_sum = arr[0], start = 0, i; ????/* Add elements one by one to? curr_sum and if the curr_sum? ???????exceeds the sum, then remove? starting element */ ????for (i = 1; i <= n; i++) { ????????// If curr_sum exceeds the sum, ????????// then remove the starting elements ????????while (curr_sum > sum && start < i - 1) { ????????????curr_sum = curr_sum - arr[start]; ????????????start++; ????????} ????????// If curr_sum becomes equal to sum, ????????// then return true ????????if (curr_sum == sum) { ????????????printf( ????????????????"Sum found between indexes %d and %d", ????????????????start, i - 1); ????????????return 1; ????????} ????????// Add this element to curr_sum ????????if (i < n) ????????????curr_sum = curr_sum + arr[i]; ????} ????// If we reach here, then no subarray ????printf("No subarray found"); ????return 0; } // Driver program to test above function int main() { ????int arr[] = { 15, 2, 4, 8, 9, 5, 10, 23 }; ????int n = sizeof(arr) / sizeof(arr[0]); ????int sum = 23; ????subArraySum(arr, n, sum); ????return 0; } ``` ## Java ```java class SubarraySum { ????/* Returns true if the there is? a subarray of arr[] with sum equal to ???????'sum' otherwise returns false.?? Also, prints the result */ ????int subArraySum(int arr[], int n, int sum) ????{ ????????int curr_sum = arr[0], start = 0, i; ????????// Pick a starting point ????????for (i = 1; i <= n; i++) { ????????????// If curr_sum exceeds the sum, ????????????// then remove the starting elements ????????????while (curr_sum > sum && start < i - 1) { ????????????????curr_sum = curr_sum - arr[start]; ????????????????start++; ????????????} ????????????// If curr_sum becomes equal to sum, ????????????// then return true ????????????if (curr_sum == sum) { ????????????????int p = i - 1; ????????????????System.out.println( ????????????????????"Sum found between indexes " + start ????????????????????+ " and " + p); ????????????????return 1; ????????????} ????????????// Add this element to curr_sum ????????????if (i < n) ????????????????curr_sum = curr_sum + arr[i]; ????????} ????????System.out.println("No subarray found"); ????????return 0; ????} ????public static void main(String[] args) ????{ ????????SubarraySum arraysum = new SubarraySum(); ????????int arr[] = { 15, 2, 4, 8, 9, 5, 10, 23 }; ????????int n = arr.length; ????????int sum = 23; ????????arraysum.subArraySum(arr, n, sum); ????} } // This code has been contributed by Mayank Jaiswal(mayank_24) ``` ## Python3 ```py # An efficient program? # to print subarray # with sum as given sum? # Returns true if the? # there is a subarray? # of arr[] with sum # equal to 'sum'? # otherwise returns? # false. Also, prints? # the result. def subArraySum(arr, n, sum): ????# Initialize curr_sum as ????# value of first element ????# and starting point as 0? ????curr_sum = arr[0] ????start = 0 ????# Add elements one by? ????# one to curr_sum and? ????# if the curr_sum exceeds? ????# the sum, then remove? ????# starting element? ????i = 1 ????while i <= n: ????????# If curr_sum exceeds ????????# the sum, then remove ????????# the starting elements ????????while curr_sum > sum and start < i-1: ????????????curr_sum = curr_sum - arr[start] ????????????start += 1 ????????# If curr_sum becomes ????????# equal to sum, then ????????# return true ????????if curr_sum == sum: ????????????print ("Sum found between indexes") ????????????print ("% d and % d"%(start, i-1)) ????????????return 1 ????????# Add this element? ????????# to curr_sum ????????if i < n: ????????????curr_sum = curr_sum + arr[i] ????????i += 1 ????# If we reach here,? ????# then no subarray ????print ("No subarray found") ????return 0 # Driver program arr = [15, 2, 4, 8, 9, 5, 10, 23] n = len(arr) sum = 23 subArraySum(arr, n, sum) # This code is Contributed by shreyanshi_arun. ``` ## C# ``` // An efficient C# program to print // subarray with sum as given sum using System; class GFG { ????// Returns true if the ????// there is a subarray of ????// arr[] with sum equal to ????// 'sum' otherwise returns false. ????// Also, prints the result ????int subArraySum(int[] arr, int n, ????????????????????int sum) ????{ ????????int curr_sum = arr[0], ????????????start = 0, i; ????????// Pick a starting point ????????for (i = 1; i <= n; i++) { ????????????// If curr_sum exceeds ????????????// the sum, then remove ????????????// the starting elements ????????????while (curr_sum > sum && start < i - 1) { ????????????????curr_sum = curr_sum - arr[start]; ????????????????start++; ????????????} ????????????// If curr_sum becomes equal to ????????????// sum, then return true ????????????if (curr_sum == sum) { ????????????????int p = i - 1; ????????????????Console.WriteLine("Sum found between " ??????????????????????????????????+ "indexes " + start + " and " + p); ????????????????return 1; ????????????} ????????????// Add this element to curr_sum ????????????if (i < n) ????????????????curr_sum = curr_sum + arr[i]; ????????} ????????Console.WriteLine("No subarray found"); ????????return 0; ????} ????// Driver code ????public static void Main() ????{ ????????GFG arraysum = new GFG(); ????????int[] arr = new int[] { 15, 2, 4, 8, ????????????????????????????????9, 5, 10, 23 }; ????????int n = arr.Length; ????????int sum = 23; ????????arraysum.subArraySum(arr, n, sum); ????} } // This code has been contributed by KRV. ``` ## PHP ```php <?php /* An efficient program to print? subarray with sum as given sum */ /* Returns true if the there is a? subarray of arr[] with sum equal? to 'sum' otherwise returns false.? Also, prints the result */ function subArraySum($arr, $n, $sum) { ????/* Initialize curr_sum as? ????value of first element ????and starting point as 0 */ ????$curr_sum = $arr[0];? ????$start = 0; $i; ????/* Add elements one by one to? ????curr_sum and if the curr_sum? ????exceeds the sum, then remove ????starting element */ ????for ($i = 1; $i <= $n; $i++) ????{ ????????// If curr_sum exceeds the sum,? ????????// then remove the starting elements ????????while ($curr_sum > $sum and? ???????????????$start < $i - 1) ????????{ ????????????$curr_sum = $curr_sum -? ????????????????????????$arr[$start]; ????????????$start++; ????????} ????????// If curr_sum becomes equal? ????????// to sum, then return true ????????if ($curr_sum == $sum) ????????{ ????????????echo "Sum found between indexes", ?????????????????????????????" ", $start, " ",? ???????????????????????????"and ", " ", $i - 1; ????????????return 1; ????????} ????????// Add this element ????????// to curr_sum ????????if ($i < $n) ????????$curr_sum = $curr_sum + $arr[$i]; ????} ????// If we reach here, ????// then no subarray ????echo "No subarray found"; ????return 0; } // Driver Code $arr = array(15, 2, 4, 8,? ??????????????9, 5, 10, 23); $n = count($arr); $sum = 23; subArraySum($arr, $n, $sum); // This code has been // contributed by anuj_67\. ?> ``` **輸出**: ``` Sum found between indexes 1 and 4 ``` **Complexity Analysis:** * **時間復雜度**:`O(n)`。 僅需要遍歷數組一次。 因此,時間復雜度為`O(n)`。 * **空間復雜度**:`O(1)`。 由于需要恒定的額外空間。 上述解決方案無法處理負數。 我們可以使用哈希處理負數。 請參閱下面的第 2 組。 * [查找具有給定總和的子數組| 系列 2(處理負數)](https://www.geeksforgeeks.org/find-subarray-with-given-sum-in-array-of-integers/) * [查找具有給定總和且在恒定空間中允許帶有負數的子數組](https://www.geeksforgeeks.org/find-subarray-with-given-sum-with-negatives-allowed-in-constant-space/)
                  <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>

                              哎呀哎呀视频在线观看