Skip to content

Conversation

@asrithaMulugoju
Copy link

@asrithaMulugoju asrithaMulugoju commented Jul 18, 2024

PR Description

Theory

Kadane's Algorithm is used to find the maximum sum of a contiguous subarray within a one-dimensional numerical array of integers. It efficiently handles both positive and negative numbers and is known for its linear time complexity.

Algorithm

  1. Initialization:

    • Initialize two variables: max_current to store the maximum sum of the subarray ending at the current position, and max_global to store the maximum sum found so far across all subarrays.
  2. Iterate through the array:

    • For each element in the array:

      • Update max_current to be the maximum of the current element itself or the sum of max_current and the current element (this decides whether to start a new subarray or to continue the existing one).

      • Update max_global to be the maximum of max_global and max_current.

  3. Result:

    • After iterating through the array, max_global will hold the maximum sum of any contiguous subarray.

Time Complexity

O(n): The algorithm runs in linear time because it makes a single pass through the array.

Space Complexity

O(1): The algorithm uses only a constant amount of extra space.

Related Issues

-Fixes #1361

  • Closes #

Checklist

  • I have gone through the contributing guide
  • I have updated my branch and synced it with project main branch before making this PR

Undertaking

I declare that:

  1. The content I am submitting is original and has not been plagiarized.
  2. No portion of the work has been copied from any other source without proper attribution.
  3. The work has been checked for plagiarism, and I assure its authenticity.

I understand that any violation of this undertaking may have legal consequences that I will bear and could result in the withdrawal of any recognition associated with the work.

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

2 participants