Skip to content

Conversation

@Harsh-Kumar-Mishra2006
Copy link

Overview

This PR adds an implementation of Kadane's Algorithm, an efficient dynamic programming approach to solve the Maximum Subarray Sum problem in O(n) time.

Details

  • Problem: Find the contiguous subarray with the maximum sum within a given one-dimensional array of numbers.
  • Approach: Dynamic Programming (Kadane's Algorithm)
  • Time Complexity: O(n)
  • Space Complexity: O(1)

Implementation Highlights

  • Implements both basic and enhanced versions of Kadane's Algorithm
  • Basic version returns only the maximum sum
  • Enhanced version returns the sum along with subarray indices
  • Includes comprehensive test cases covering various scenarios
  • Handles edge cases: empty arrays, all negative numbers, single elements
  • Clean, commented, and easy-to-follow C++ code

Example

Input: [-2, 1, -3, 4, -1, 2, 1, -5, 4]
Output: 6 (Subarray: [4, -1, 2, 1])

Files Added

  • algorithms/kadane_algorithm.cpp - Complete implementation with test cases

Key Features

  • ✅ Follows dynamic programming approach as requested in issue Add Kadane's Algorithm for Maximum Subarray Sum (DP Approach) #25
  • ✅ O(n) time complexity with O(1) space complexity
  • ✅ Comprehensive test suite with multiple edge cases
  • ✅ Well-documented code with clear comments
  • ✅ Enhanced version that can return the actual subarray

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant