maximum subarray sum in c++
C++
# Python program to find maximum contiguous subarray
def maxSubArraySum(a,size):
max_so_far =a[0]
curr_max = a[0]
for i in range(1,size):
curr_max = max(a[i], curr_max + a[i])
max_so_far = max(max_so_far,curr_max)
return max_so_far
# Driver function to check the above function
a = [-2, -3, 4, -1, -2, 1, 5, -3]
print"Maximum contiguous sum is" , maxSubArraySum(a,len(a))
#This code is contributed by _Devesh Agrawal_
int maxsubarraysum(vector<int> ar) {
int best = 0, sum = 0;
for (int i=0; i<ar.size(); i++) {
sum = max(ar[i], sum + ar[i]);
best = max(best, sum);
}
return best;
}public int kadane(int[] arr){
int max_so_far = 0, curr_max = Integer.MIN_VALUE;
for(int i: arr){
max_so_far += i;
if(max_so_far<0) max_so_far = 0;
if(max_so_far>curr_max) curr_max = max_so_far;
}
return curr_max;
}
Also in C++:
- Title
- sqrt in c++
- Category
- C++
- Title
- eigenvalue of matrix c++ using Eigen
- Category
- C++
- Title
- c++ parse int
- Category
- C++
- Title
- leveling system c++
- Category
- C++
- Title
- c++ while true
- Category
- C++
- Title
- linear search in c++
- Category
- C++
- Title
- rand c++
- Category
- C++
- Title
- c++ write string
- Category
- C++
- Title
- for loop
- Category
- C++
- Title
- c++ typeid get type name
- Category
- C++
- Title
- how to find the index of an element in a vector c++
- Category
- C++
- Title
- c++ files
- Category
- C++
- Title
- primitive and non primitive data types in c++
- Category
- C++
- Title
- c++ how to return an empty vector
- Category
- C++
- Title
- 2d vector
- Category
- C++
- Title
- maximum possible number atmost k swaps
- Category
- C++
- Title
- screen record ios simulator
- Category
- C++
- Title
- declaration vs. definition cpp
- Category
- C++
- Title
- how to create object in c++
- Category
- C++
- Title
- c++ triple
- Category
- C++
- Title
- lopping over an array c++
- Category
- C++
- Title
- extends c++
- Category
- C++
- Title
- how to switch to another branch in git
- Category
- C++
- Title
- declare vectors c++
- Category
- C++
- Title
- what is order in of preeendence in float, int, char, bool
- Category
- C++
- Title
- programa para saber si un numero es primo
- Category
- C++
- Title
- c++ comment
- Category
- C++
- Title
- create new file c++
- Category
- C++
- Title
- c++ string to vector int
- Category
- C++
- Title
- how to convert qt string to string
- Category
- C++
- Title
- converting char to int in c++
- Category
- C++
- Title
- how to convert n space separated integers in c++
- Category
- C++
- Title
- c++ function return pointer to itself
- Category
- C++
- Title
- #include
- Category
- C++
- Title
- iterar un map c++
- Category
- C++
- Title
- c++ find object in vector by attribute
- Category
- C++
- Title
- stl sort in c++
- Category
- C++
- Title
- c++ switch
- Category
- C++
- Title
- c++ give options
- Category
- C++
- Title
- sum of integer in array c++
- Category
- C++
- Title
- least number of coins to form a sum
- Category
- C++
- Title
- how to make a switch case statement in c++
- Category
- C++
- Title
- list conda environments
- Category
- C++
- Title
- shortest path with bfs in c++
- Category
- C++
- Title
- C++ and endl
- Category
- C++
- Title
- c++ empty stream
- Category
- C++
- Title
- constant variables in c++
- Category
- C++
- Title
- range of int
- Category
- C++
- Title
- c++ argv
- Category
- C++
- Title
- how to find the mode of a vector c++
- Category
- C++
- Title
- euler's totient function c++
- Category
- C++
- Title
- pointers in cpp
- Category
- C++
- Title
- nan c++ example
- Category
- C++
- Title
- c++ uint32_t
- Category
- C++
- Title
- variabili in c++
- Category
- C++
- Title
- how to write an or in c++
- Category
- C++
- Title
- getch c++ library
- Category
- C++
- Title
- c++ call method in same class
- Category
- C++
- Title
- capitalize first letter c++
- Category
- C++
- Title
- widechartomultibyte
- Category
- C++
- Title
- c++ for loop syntax
- Category
- C++
- Title
- how to print to the serial monitor arduino
- Category
- C++
- Title
- find number of 1s in a binary cv::mat image
- Category
- C++
- Title
- c++ functions
- Category
- C++
- Title
- subarray sum in c++
- Category
- C++
- Title
- c++ random
- Category
- C++
- Title
- roscpp publish int32
- Category
- C++
- Title
- solve linear equations geeksforgeeks
- Category
- C++
- Title
- how to reverse a vector
- Category
- C++
- Title
- visual studio 2019 c++ tutorial project
- Category
- C++
- Title
- initialising 2d vector
- Category
- C++
- Title
- Runtime Error: Runtime ErrorFloating-point exception (SIGFPE
- Category
- C++
- Title
- convert int to binary string c++
- Category
- C++
- Title
- new in c++
- Category
- C++
- Title
- c++ multiple inheritance diamond problem
- Category
- C++
- Title
- cube mapping sdl
- Category
- C++
- Title
- insert function in c++ vector
- Category
- C++
- Title
- function template
- Category
- C++
- Title
- c++ how to make a negative float positive
- Category
- C++
- Title
- c++ vector
- Category
- C++
- Title
- initialize 3d vector c++
- Category
- C++
- Title
- set of vectors c++
- Category
- C++
- Title
- c++ for loop
- Category
- C++
- Title
- stoi c++
- Category
- C++
- Title
- hobo 8
- Category
- C++
- Title
- pause the console c++
- Category
- C++
- Title
- How to find the suarray with maximum sum using divide and conquer
- Category
- C++
- Title
- c++ char to string
- Category
- C++
- Title
- c++ char print fixed
- Category
- C++
- Title
- time conversion hackerrank solution in c++
- Category
- C++
- Title
- c++ program for addition of two numbers using functions
- Category
- C++
- Title
- can you use a return to print a string when referencing an integer c++
- Category
- C++
- Title
- new class * [] c++
- Category
- C++
- Title
- templates of templates c++
- Category
- C++
- Title
- c++ convert const char* to LPCWSTR
- Category
- C++
- Title
- lambda c++
- Category
- C++
- Title
- C++ w3schools
- Category
- C++
- Title
- c++ set add element
- Category
- C++
- Title
- use of strstr in c++
- Category
- C++
- Title
- Find the minimum difference between pairs in a simple path of tree C++
- Category
- C++