
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Maximum Sum SubArray using Divide and Conquer in C++
In this article, we have an array of integers. Our task is to find the maximum sum of a sub-array using divide and conquer algorithm. A sub-array is a continuous part of an array with consecutive array elements. For example: {2,3,4} is sub-array of {1,2,3,4,5} but {1,4,5} is not.
What is Divide and Conquer Algorithm?
The Divide and Conquer algorithm divides a problem into smaller sub-problems, and then each sub-problem is solved independently. We keep dividing the sub-problems till we reach a stage where no more division is possible. Those smallest possible sub-problems are then solved, and the solution of all sub-problems is finally merged in order to obtain the solution of the original problem. Here are some example scenarios of the given problem:
Scenario 1
Input: arr = {4, -3, 6, -8, 9, 1} Output: 10 Explanation: The subarray {9, 1} gives the maximum sum as 10.
Scenario 2
Input: arr = {8, -9, 6, 3, 2, -4, 6, 1} Output: 14 Explanation: The subarray {6, 3, 2, -4, 6, 1} gives the maximum sum as 14.
Steps to Find Maximum Subarray Sum using Divide and Conquer
The steps for maximum subarray sum are given below using divide and conquer algorithm:
- Find the middle index of the array.
- Divide the array into left and right halves around the middle element.
- The maxSum() function is recursively called only to search for the maximum sum in the left and right subarrays, respectively.
- It divides the left and right subarrays until the left and right indices become equal(until 1 element remains in the subarray). The left element is then returned.
- The maxCrossSum() function calculates the maximum sum starting from the left half to the right half.
- It calculates the leftSum starting from the middle element to the first element and rightSum from the (middle+1st) element to the last element. The max() function gets the maximum sum from both halves.
- The maximum sum from both halves is then added and returned, and using the max() function, we get the maximum subarray sum from the left half, right half, and cross sum.
C++ Program to Find Maximum Subarray Sum using Divide and Conquer
Here is the code implementation of the steps mentioned above to get the maximum subarray sum:
#include <iostream> #include <climits> #include <algorithm> using namespace std; int maxCrossSum(int arr[], int left, int mid, int right) { int sum = 0; int leftSum = INT_MIN; for (int i = mid; i >= left; i--) { sum += arr[i]; leftSum = max(leftSum, sum); } sum = 0; int rightSum = INT_MIN; for (int i = mid + 1; i <= right; i++) { sum += arr[i]; rightSum = max(rightSum, sum); } return leftSum + rightSum; } int maxSum(int arr[], int left, int right) { if (left == right) return arr[left]; int mid = (left + right) / 2; int leftMax = maxSum(arr, left, mid); int rightMax = maxSum(arr, mid + 1, right); int crossSum = maxCrossSum(arr, left, mid, right); return max({leftMax, rightMax, crossSum}); } int main() { int arr[] = {-2, -5, 6, -2, -3, 1, 5, -6}; int n = sizeof(arr) / sizeof(arr[0]); cout << "Given array: "; for (int i = 0; i < n; i++) { cout << arr[i] << " "; } int max_sum = maxSum(arr, 0, n - 1); cout << "\nMaximum Subarray Sum: " << max_sum << endl; return 0; }
The output of the above code is as follows:
Given array: -2 -5 6 -2 -3 1 5 -6 Maximum Subarray Sum: 7
The time and space complexity of the above code is O(n log n) and O(log n), respectively.