💻
Software Development
DSA
DSA
  • Data structure
    • Bit
      • MSB & LSB
  • Arrays
  • Linked List
  • Stack
  • Queue
  • Hash Table
  • Tree
  • Graph
  • Algorithm
    • Bitwise
      • Challenges
    • Array
      • Two pointer
      • Sliding Window
      • Kadane Algorithm
      • Naive Pattern Searching
      • Knuth-Morris-Pratt Algorithm (KMP)
      • Quick Select Algorithm
      • Boyer - Moore Majority Vote Algorithm
      • Floyd's Cycle Detection Algorithm
    • Linked List
      • Challenges
    • Searching
      • Binary Search
        • Challenges
      • Linear Search
      • Depth First Search
      • Breadth First Search
      • Fuzzy Search
      • Rabin-Karp Algorithm
      • A* Algorithm
      • Z Algorithm
    • Sorting
      • Dutch National Flag Algorithm
    • String
      • Manacher Algorithm
    • Gready
      • Knapsack Problem Algorithms
    • Backtracking
    • Tree
      • Moris Traversal
        • Preorder
        • Inorder
    • Graph
      • Biconnected Components
        • Tarjan's Algorithm
      • Kruskal's Algorithm
      • Bellman Ford Algorithm
      • Floyd Warshall Algorithm
      • Topological Sort Algorithm
      • Flood Fill Algorithm
      • Lee Algorithm
      • Boruvka's Algorithm
      • Johnson's Algorithm
      • Kosaraju's Algorithm
    • Rate limitting
      • Token bucket
      • Leaking bucket
      • Fixed window counter
      • Sliding window log
      • Sliding window counter
    • Prioroty Queue
      • Dijkstra's Algorithm
      • Huffman Coding Compression Algorithm
      • Prim's Algorithm
      • kth largest element in an array
    • More
      • Dynamic Programming
      • Pigeonhole Principle
        • Challenges
      • Euclidean algorithm
      • Brian Kernighan’s algorithm
      • Sieve of Eratosthenes Algorithm
      • Sieve of Atkin
      • Luhn Algorithm
      • Maximizing XOR
    • Some pattern to remember when facing the challenges
Powered by GitBook
On this page
  1. Algorithm
  2. Array

Kadane Algorithm

PreviousSliding WindowNextNaive Pattern Searching

Last updated 19 days ago

Classic Problem: Finding the largest subarray

Explain:

Leetcode

=> PATTERN: Remember this code

def max_subarray(A):
    max_current = max_global = nums[0]

    for num in nums[1:]:
        max_current = max(num, max_current + num)
        max_global = max(max_global, max_current)

    return max_global

Enhancement: Now we have maximum subarray. Now Calculate maximum subsequence from this subarray.

For example: The subarray [2, -1, 2, 3, 4] is the subarray with the maximum sum, and [2, 2, 3, 4] is the subsequence with the maximum sum.

=> PATTERN: Remember this code

def max_subarray(A):
    max_current = max_global = max_subsequence = nums[0]

    for num in nums[1:]:
        max_current = max(num, max_current + num)
        max_global = max(max_global, max_current)
        max_subsequence = max(max_subsequence, max_subsequence + num, num)

    return max_global, max_subsequence

ADVANCE: Find that subarray

=> Remember this code

def max_subarray(nums):
    max_sum = nums[0]
    start = end = 0
    current_sum = nums[0]
    current_start = 0

    for i in range(1, len(nums)):
        if nums[i] > current_sum + nums[i]:
            current_sum = nums[i]
            current_start = i
        else:
            current_sum += nums[i]

        if current_sum > max_sum:
            max_sum = current_sum
            start = current_start
            end = i

    return nums[start:end+1]
https://www.geeksforgeeks.org/largest-sum-contiguous-subarray
Loading...LeetCode
Loading...LeetCode
Logo
Logo