Write an efficient C program to find the sum of contiguous subarray within a one-dimensionalarray of integer which returns the largest sum.Explanation:Lets take the example of array {5,-3, 4}Possible contiguous subarray combinations are{5}, {-3}, {4}, {5,-3}, {-3,4}, {5,-3,4}The contiguous subarray {5,-3,4} has got the largest sum 6
Question
Write an efficient C program to find the sum of contiguous subarray within a one-dimensionalarray of integer which returns the largest sum.Explanation:Lets take the example of array {5,-3, 4}Possible contiguous subarray combinations are{5}, {-3}, {4}, {5,-3}, {-3,4}, {5,-3,4}The contiguous subarray {5,-3,4} has got the largest sum 6
Solution
Sure, here is a C program that uses Kadane's algorithm to find the sum of the contiguous subarray within a one-dimensional array of integers which returns the largest sum.
#include<stdio.h>
int maxSubArraySum(int a[], int size)
{
int max_so_far = a[0];
int curr_max = a[0];
for (int i = 1; i < size; i++)
{
curr_max = max(a[i], curr_max+a[i]);
max_so_far = max(max_so_far, curr_max);
}
return max_so_far;
}
int max(int x, int y)
{
return (x > y)? x : y;
}
int main()
{
int a[] = {5, -3, 4};
int n = sizeof(a)/sizeof(a[0]);
int max_sum = maxSubArraySum(a, n);
printf("Maximum contiguous sum is %d\n", max_sum);
return 0;
}
This program first initializes max_so_far and curr_max to the first element of the array. Then for each element in the array (starting from the second), it updates curr_max to be the maximum of the current element and the sum of the current element and curr_max. It also updates max_so_far to be the maximum of max_so_far and curr_max. Finally, it returns max_so_far which is the maximum sum of a contiguous subarray.
Similar Questions
Given an integer array arr, write a program to find the subarray which has the largest sum and return its sum.Sample Input:9-2 -1 -3 4 -1 2 1 -5 4Sample Output:6Explanation:The subarray is [4, -1, 2, 1] and the sum is 6
Given an integer array arr, partition the array into (contiguous) subarrays of length at most k. After partitioning, each subarray has their values changed to become the maximum value of that subarray.Return the largest sum of the given array after partitioning. Test cases are generated so that the answer fits in a 32-bit integer. Example 1:Input: arr = [1,15,7,9,2,5,10], k = 3Output: 84Explanation: arr becomes [15,15,15,9,10,10,10]
Given an integer array nums, find the subarray with the largest sum, and return its sum.
Given an array 𝐴A of size 𝑁N, find the size of the largest subarray of 𝐴A which has an even sum.For example, for the array [2,1,2][2,1,2] the answer is just 11 as the largest subarray with an even sum is [2][2].Note that a subarray is a contiguous part of an array. For the array [1,3,2][1,3,2], some possible subarrays are [1],[2],[1,3],[1,3,2][1],[2],[1,3],[1,3,2]. However, [1,2][1,2] is not a subarray for this array.Input FormatThe first line of input will contain a single integer 𝑇T, denoting the number of test cases.Each test case consists of multiple lines of input.The first line of each test case contains a single integer 𝑁N — the size of the array.The next line contains 𝑁N space-separated integer - 𝐴1,𝐴2,…,𝐴𝑁A 1 ,A 2 ,…,A N .Output FormatFor each test case, output on a new line, the size of the largest subarray with even sum.Constraints1≤𝑇≤501≤T≤501≤𝑁≤501≤N≤501≤𝐴𝑖≤1041≤A i ≤10 4 Sample 1:InputOutput432 1 231 3 262 2 1 1 1 2111340Explanation:Test case 11: The largest subarray with even sum is the subarray [2][2] having size 11.Test case 22: The largest subarray with even sum is the subarray [1,3,2][1,3,2] having size 33.Test case 33: The largest subarray with even sum is the subarray [2,2,1,1][2,2,1,1] having size 44.Test case 44: No subarray has even sum.
Q: 02 of 15Consider the array [-2, 1, -3, 4, -1, 2, 1, -5, 4]. Using the Divide and Conquer approach, which of the following subarray generates the maximum subarray sum?Options :LSSCSSRSSNone of these
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.