Find subarray with given sum
2015-01-15 05:32
267 查看
Given an unsorted array of nonnegative integers, find a continous subarray which adds to a given number. Examples:
Input: arr[] = {1, 4, 20, 3, 10, 5}, sum = 33
Ouptut: Sum found between indexes 2 and 4
Input: arr[] = {1, 4, 0, 0, 3, 10, 5}, sum = 7
Ouptut: Sum found between indexes 1 and 4
Input: arr[] = {1, 4}, sum = 0
Output: No subarray found.
There may be more than one subarrays with sum as the given sum. The following solutions print first such subarray.
思路:用fast和slow两个下标,计算fast和slow之间的subarray的和,如果和大于给定值,那么把slow向前移动一位,如果和小于给定值,那么把fast向前移动一位。
代码:
Input: arr[] = {1, 4, 20, 3, 10, 5}, sum = 33
Ouptut: Sum found between indexes 2 and 4
Input: arr[] = {1, 4, 0, 0, 3, 10, 5}, sum = 7
Ouptut: Sum found between indexes 1 and 4
Input: arr[] = {1, 4}, sum = 0
Output: No subarray found.
There may be more than one subarrays with sum as the given sum. The following solutions print first such subarray.
思路:用fast和slow两个下标,计算fast和slow之间的subarray的和,如果和大于给定值,那么把slow向前移动一位,如果和小于给定值,那么把fast向前移动一位。
代码:
/* 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) { /* 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; }
相关文章推荐
- Find subarray with given sum
- Given an array of positive and negative integers find the first subarray with zero sum
- Find subarray with given sum
- Data Structure Array: Find if there is a subarray with 0 sum
- Smallest subarray with sum greater than a given value
- 在二叉搜索树里,寻找两个节点 Find a pair with given sum in a Balanced BST
- Smallest subarray with sum greater than a given value
- Given a tree, find the node with the minimum sum of distances to other nodes
- Find K Pairs with Smallest Sum
- Kadane’s Algorithm to find maximum subarray sum
- No. 09 - Numbers with a Given Sum
- Implement Find and replace (find given pattern and replace it with a given string)
- *Find All Repeating Substring With Given Length
- CareerCup Given an array having positive integers, find a subarray which adds to a given number
- Find the maximum subarray XOR in a given array
- Subarray Sum to the given target value
- Find the subarray with least average
- Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive
- Find the longest substring with k unique distinct characters in a given string (Google)
- Maximum Subarray with Sum/ Multiply