Skip to content

Kadane algorithm#165

Merged
admirerr merged 2 commits into
admirerr:mainfrom
SARATH-ANSIL:kadaneAlgorithm
Oct 8, 2025
Merged

Kadane algorithm#165
admirerr merged 2 commits into
admirerr:mainfrom
SARATH-ANSIL:kadaneAlgorithm

Conversation

@SARATH-ANSIL
Copy link
Copy Markdown

Problem:

Find the contiguous subarray within a one-dimensional array of numbers that has the largest sum.

Idea / Intuition

While traversing the array, keep track of two things:

  1. Current maximum subarray sum ending at this position (maxEndingHere)
  2. Overall maximum sum found so far (maxSoFar)

At each element, we have two choices:

  1. Start a new subarray at the current element if the previous sum is negative.
  2. Extend the current subarray by including the current element.

### Formula:
maxEndingHere = max(currentElement, maxEndingHere + currentElement)
maxSoFar = max(maxSoFar, maxEndingHere)

Step-by-Step Logic

  1. Initialize maxSoFar and maxEndingHere with the first element of the array.
  2. Loop through the array from the second element:
  3. Update maxEndingHere using the formula above.
  4. Update maxSoFar if maxEndingHere is greater.
  5. After traversing the array, maxSoFar contains the maximum subarray sum.

### Why It Works

  1. If the sum ending at the previous element is negative, it cannot contribute to a larger sum in the future, so we start fresh.
  2. If the sum is positive, including the current element will increase the subarray sum, so we continue.

Time and Space Complexity

Time Complexity: O(n) – single pass through the array.

Space Complexity: O(1) – only two variables are needed.

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Oct 7, 2025

🎉 Welcome to Hacktoberfest 2025, @SARATH-ANSIL! 🎃

Thank you for your first contribution to our DSA repository! Here's what happens next:

🔍 Automatic Checks

  • Code Validation: Passed
  • 🧪 Compilation Tests: Passed

📋 Next Steps

🎯 Great job! Your code compiled successfully. Maintainers @admirerr and @kartikey2991 will review your PR soon.

🎁 What You Get

  • 🏆 Hacktoberfest Credit: This PR counts toward your 6 PR goal for exclusive T-shirt + Tree!
  • 🌟 Hall of Fame: You'll be featured in our contributors list
  • 📚 Learning: Code review feedback from experienced developers

💡 Tips for Success

  • Follow our Contributing Guidelines
  • Add comments explaining your algorithm
  • Include time/space complexity analysis
  • Test your code before submitting

Welcome to the community! 🚀

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Oct 7, 2025

🤖 Automated PR Status

🔍 Code Validation

Passed - File naming and structure look good!

🧪 Compilation Tests

Passed - All code compiles successfully!

📋 Overall Status

🎉 Ready for Review - Your PR has passed all automated checks!
👥 Maintainers have been notified for review.


This comment was generated automatically. Checks will re-run when you push new commits.

@admirerr
Copy link
Copy Markdown
Owner

admirerr commented Oct 8, 2025

Approved

@admirerr admirerr merged commit 87464f5 into admirerr:main Oct 8, 2025
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants