
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Maximum Subarray Sum using Divide and Conquer algorithm in C++
Suppose we have one list of data with positive and negative values. We have to find the sum of contiguous subarray whose sum is largest. Suppose the list is containing {-2, -5, 6, -2, -3, 1, 5, -6}, then the sum of maximum subarray is 7. It is the sum of {6, -2, -3, 1, 5}
We will solve this problem by using the Divide and Conquer method. The steps will look like below −
Steps −
- Divide the array into two parts
- Find the maximum of the following three
- Maximum subarray sum of left subarray
- Maximum subarray sum of right subarray
- Maximum subarray sum such that subarray crosses the midpoint
Example
#include <iostream> using namespace std; int max(int a, int b) { return (a > b)? a : b; } int max(int a, int b, int c) { return max(max(a, b), c); } int getMaxCrossingSum(int arr[], int l, int m, int h) { int sum = 0; int left = INT_MIN; for (int i = m; i >= l; i--) { sum = sum + arr[i]; if (sum > left) left = sum; } sum = 0; int right = INT_MIN; for (int i = m+1; i <= h; i++) { sum = sum + arr[i]; if (sum > right) right = sum; } return left + right; } int maxSubArraySum(int arr[], int low, int high) { if (low == high) return arr[low]; int mid = (low + high)/2; return max(maxSubArraySum(arr, low, mid), maxSubArraySum(arr, mid+1, high), getMaxCrossingSum(arr, low, mid, high)); } int main() { int arr[] = {-2, -5, 6, -2, -3, 1, 5, -6}; int n = sizeof(arr)/sizeof(arr[0]); int max_sum = maxSubArraySum(arr, 0, n-1); printf("Maximum contiguous sum is %d", max_sum); }
Output
Valid String
- Related Articles
- Maximum Sum SubArray using Divide and Conquer in C++
- Python Program to solve Maximum Subarray Problem using Divide and Conquer
- Convex Hull using Divide and Conquer Algorithm in C++
- Using Kadane’s algorithm to find maximum sum of subarray in JavaScript
- Maximum subarray sum in circular array using JavaScript
- Python Program to solve Maximum Subarray Problem using Kadane’s Algorithm
- Maximum subarray sum in O(n) using prefix sum in C++
- Introduction to Divide & Conquer Algorithms
- Maximum Sum Circular Subarray in C++
- Maximum sum bitonic subarray in C++
- Program to find maximum ascending subarray sum using Python
- Advanced master theorem for divide and conquer recurrences
- Maximum subarray sum modulo m in C++
- Maximum circular subarray sum in C++\n
- Maximum contiguous sum of subarray in JavaScript

Advertisements