How to find the suarray with maximum sum using divide and conquer
C++
#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;
}
Also in C++:
- Title
- c++ not greater than
- Category
- C++
- Title
- getch c++ library
- Category
- C++
- Title
- hohw toparse a string in c++
- Category
- C++
- Title
- swapo algorit
- Category
- C++
- Title
- c++ try
- Category
- C++
- Title
- howt o initialize 3d vector in c++
- Category
- C++
- Title
- c++ max of array
- Category
- C++
- Title
- how to input multiple lines of a file in c++
- Category
- C++
- Title
- least number of coins to form a sum
- Category
- C++
- Title
- random number generator c++
- Category
- C++
- Title
- chess perft 5
- Category
- C++
- Title
- c++ function to find length of array
- Category
- C++
- Title
- count function c++
- Category
- C++
- Title
- dynamic 2d array c++
- Category
- C++
- Title
- memcmp in cpp
- Category
- C++
- Title
- sort a pair using c++ stl
- Category
- C++
- Title
- c++ looping through a vector
- Category
- C++
- Title
- preorder traversal c++
- Category
- C++
- Title
- primeros numeors primos menores que
- Category
- C++
- Title
- How to find the suarray with maximum sum using divide and conquer
- Category
- C++
- Title
- lambda operator in c++
- Category
- C++
- Title
- how to turn int into string c++
- Category
- C++
- Title
- c++ switch
- Category
- C++
- Title
- variadic templates
- Category
- C++
- Title
- first fit algorithm
- Category
- C++
- Title
- new c++
- Category
- C++
- Title
- floor() in c++
- Category
- C++
- Title
- Temporary file using MSFT API in cpp
- Category
- C++
- Title
- c++ typeid get type name
- Category
- C++
- Title
- index string c++
- Category
- C++
- Title
- dfs in c++
- Category
- C++
- Title
- how to concatinate two strings in c++
- Category
- C++
- Title
- calling by reference c++
- Category
- C++
- Title
- c++ how to skip the last element of vector
- Category
- C++
- Title
- to_string c++
- Category
- C++
- Title
- how to output to console c++
- Category
- C++
- Title
- and or in c++
- Category
- C++
- Title
- c++ argv
- Category
- C++
- Title
- how to print eachh chars in string data type in c++
- Category
- C++
- Title
- declaring vector c++
- Category
- C++
- Title
- array 2d to 1d
- Category
- C++
- Title
- c++ create object
- Category
- C++
- Title
- c++ convert int to double
- Category
- C++
- Title
- c++ program for addition of two numbers using functions
- Category
- C++
- Title
- single line if c++
- Category
- C++
- Title
- clear console c++
- Category
- C++
- Title
- basic ex of maps in c++
- Category
- C++
- Title
- how to make a switch case statement in c++
- Category
- C++
- Title
- how to get the prime number in c++ where time complexity is 0(log n)
- Category
- C++
- Title
- bellman ford algorithm cp algorithm
- Category
- C++
- Title
- pairs in c++
- Category
- C++
- Title
- c++ how to return an empty vector
- Category
- C++
- Title
- gfg right view of tree
- Category
- C++
- Title
- pop from between string c++
- Category
- C++
- Title
- split 2d array into chunks in c++
- Category
- C++
- Title
- subarray sum in c++
- Category
- C++
- Title
- c++ clamp
- Category
- C++
- Title
- c++ write new file
- Category
- C++
- Title
- c++ rainbow text
- Category
- C++
- Title
- run program until ctrl-d c++
- Category
- C++
- Title
- set c++
- Category
- C++
- Title
- 1d fixed length arrays c++
- Category
- C++
- Title
- c++ set console title
- Category
- C++
- Title
- insertion c++
- Category
- C++
- Title
- sort function in vector c++ stl
- Category
- C++
- Title
- c++ declare char
- Category
- C++
- Title
- mkdir boost filesystem
- Category
- C++
- Title
- C++ w3schools
- Category
- C++
- Title
- quick sort predefined function in c++
- Category
- C++
- Title
- c++ string to stream
- Category
- C++
- Title
- c++ enum rand
- Category
- C++
- Title
- How to find the kth smallest number in cinstant space
- Category
- C++
- Title
- c++ passing two dimensional array to function
- Category
- C++
- Title
- c++ class template
- Category
- C++
- Title
- how to create object in c++
- Category
- C++
- Title
- file objects in c++
- Category
- C++
- Title
- two sum problem in c++
- Category
- C++
- Title
- Check if a Number is Odd or Even using Bitwise Operators
- Category
- C++
- Title
- intersection between vector c++
- Category
- C++
- Title
- how to print 5 precision float in c++
- Category
- C++
- Title
- binary tree search
- Category
- C++
- Title
- digitalwrite C++
- Category
- C++
- Title
- map insert c++
- Category
- C++
- Title
- expected unqualified-id before 'if'
- Category
- C++
- Title
- how to show c++ binary files in sublime text
- Category
- C++
- Title
- how to extract substring from string in c++
- Category
- C++
- Title
- accumulate in cpp
- Category
- C++
- Title
- heredar constructor c++
- Category
- C++
- Title
- how to decalre a string in c++
- Category
- C++
- Title
- including cpp header file in c++
- Category
- C++
- Title
- gcd in c++
- Category
- C++
- Title
- iterate const vector
- Category
- C++
- Title
- how to make a n*n 2d dynamic array in c++
- Category
- C++
- Title
- c++ return multiple values
- Category
- C++
- Title
- power c++
- Category
- C++
- Title
- loop through array c++
- Category
- C++
- Title
- c++ how to add something at the start of a vector
- Category
- C++
- Title
- sum of two numbers c++
- Category
- C++
- Title
- how use global variables instead of local in c++
- Category
- C++
- Title
- Operator overloading in C++ Programming
- Category
- C++