
- 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
C++ Program to Implement Kadane’s Algorithm
Kadane’s algorithm is used to find out the maximum subarray sum from an array of integers. Here we shall discuss a C++ program to implement this algorithm.
Algorithm
Begin Function kadanes(int array[], int length): Initialize highestMax = 0 currentElementMax = 0 for i = 0 to length-1 currentElementMax = max(array[i],currentElementMax + array[i]) highestMax = max(highestMax, currentElementMax) return highestMax End
Example
#include<iostream> using namespace std; int kadanes(int array[],int length) { int highestMax = 0; int currentElementMax = 0; for(int i = 0; i < length; i++){ currentElementMax =max(array[i],currentElementMax + array[i]) ; highestMax = max(highestMax,currentElementMax); } return highestMax; } int main() { cout << "Enter the array length: "; int l; cin >> l; int arr[l]; cout << "Enter the elements of array: "; for (int i = 0; i < l; i++) { cin >> arr[i]; } cout << "The Maximum Sum is: "<<kadanes(arr,l) << endl; return 0; }
Output
Enter the array length: 7 Enter the elements of array: -1 -2 -3 -4 -5 6 7 The Maximum Sum is: 13
- Related Articles
- C program to implement Euclid’ s algorithm
- C++ Program to Implement Johnson’s Algorithm
- C++ Program to Implement the RSA Algorithm
- C++ Program to Implement Coppersmith Freivald’s Algorithm
- C++ Program to Implement Modular Exponentiation Algorithm
- C++ Program to Implement Nearest Neighbour Algorithm
- C++ Program to Implement Expression Tree Algorithm
- C++ Program to Implement Interpolation Search Algorithm
- C++ Program to Implement Extended Euclidean Algorithm
- C++ Program to Implement Levenshtein Distance Computing Algorithm
- C++ Program to Implement Dijkstra’s Algorithm Using Set
- C++ Program to Implement The Edmonds-Karp Algorithm
- C++ Program to Implement the Bin Packing Algorithm
- C++ Program to Implement Bitap Algorithm for String Matching
- C++ Program to Implement Fisher-Yates Algorithm for Array Shuffling

Advertisements