"How to find the suarray with maximum sum using divide and conquer" Code Answer's
You're definitely familiar with the best coding language C++ that developers use to develop their projects and they get all their queries like "How to find the suarray with maximum sum using divide and conquer" answered properly. Developers are finding an appropriate answer about How to find the suarray with maximum sum using divide and conquer related to the C++ coding language. By visiting this online portal developers get answers concerning C++ codes question like How to find the suarray with maximum sum using divide and conquer. Enter your desired code related query in the search bar and get every piece of information about C++ code related question on How to find the suarray with maximum sum using divide and conquer.
How to find the suarray with maximum sum using divide and conquer
#include <stdio.h>
#include <limits.h>
// Utility function to find maximum of two numbers
int max(int x, int y) {
return (x > y) ? x : y;
}
// Function to find maximum subarray sum using divide and conquer
int maximum_sum(int A[], int low, int high)
{
// If array contains only one element
if (high == low)
return A[low];
// Find middle element of the array
int mid = (low + high) / 2;
// Find maximum subarray sum for the left subarray
// including the middle element
int left_max = INT_MIN;
int sum = 0;
for (int i = mid; i >= low; i--)
{
sum += A[i];
if (sum > left_max)
left_max = sum;
}
// Find maximum subarray sum for the right subarray
// excluding the middle element
int right_max = INT_MIN;
sum = 0; // reset sum to 0
for (int i = mid + 1; i <= high; i++)
{
sum += A[i];
if (sum > right_max)
right_max = sum;
}
// Recursively find the maximum subarray sum for left subarray
// and right subarray and take maximum
int max_left_right = max(maximum_sum(A, low, mid),
maximum_sum(A, mid + 1, high));
// return maximum of the three
return max(max_left_right, left_max + right_max);
}
// Maximum Sum Subarray using Divide & Conquer
int main(void)
{
int arr[] = { 2, -4, 1, 9, -6, 7, -3 };
int n = sizeof(arr) / sizeof(arr[0]);
printf("The maximum sum of the subarray is %d",
maximum_sum(arr, 0, n - 1));
return 0;
}
Source: www.techiedelight.com
All those coders who are working on the C++ based application and are stuck on How to find the suarray with maximum sum using divide and conquer can get a collection of related answers to their query. Programmers need to enter their query on How to find the suarray with maximum sum using divide and conquer related to C++ code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about How to find the suarray with maximum sum using divide and conquer for the programmers working on C++ code while coding their module. Coders are also allowed to rectify already present answers of How to find the suarray with maximum sum using divide and conquer while working on the C++ language code. Developers can add up suggestions if they deem fit any other answer relating to "How to find the suarray with maximum sum using divide and conquer". Visit this developer's friendly online web community, CodeProZone, and get your queries like How to find the suarray with maximum sum using divide and conquer resolved professionally and stay updated to the latest C++ updates.