From 84f33a588f87e9b243a2cc53118833850d2ff394 Mon Sep 17 00:00:00 2001 From: mahletseyoum101 <118665264+mahletseyoum101@users.noreply.github.com> Date: Mon, 23 Dec 2024 18:32:51 +0300 Subject: [PATCH] kadanes_algorithm for finding the maximum sum subarray with a twist --- circular_kadne.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 circular_kadne.py diff --git a/circular_kadne.py b/circular_kadne.py new file mode 100644 index 00000000..1b5bcca2 --- /dev/null +++ b/circular_kadne.py @@ -0,0 +1,32 @@ +def kadanes_algorithm(arr): + """Standard Kadane's Algorithm for maximum sum subarray.""" + max_ending_here = max_so_far = arr[0] + + for num in arr[1:]: + max_ending_here = max(num, max_ending_here + num) + max_so_far = max(max_so_far, max_ending_here) + + return max_so_far + +def max_circular_subarray(arr): + """Kadane's Algorithm variation for circular arrays.""" + max_kadane = kadanes_algorithm(arr) + + # Compute total array sum + total_sum = sum(arr) + + # Invert the array elements + inverted_arr = [-x for x in arr] + + # Find the maximum sum of the inverted array (minimum subarray sum) + max_inverted_kadane = kadanes_algorithm(inverted_arr) + + # The maximum circular sum is total_sum + max_inverted_kadane + # If max_inverted_kadane is equal to total_sum, it means all elements are negative + max_circular = total_sum + max_inverted_kadane if max_inverted_kadane != -total_sum else max_kadane + + return max(max_kadane, max_circular) + +# Example usage +arr = [1, -2, 4, -3] +print("Maximum sum subarray (circular):", max_circular_subarray(arr)) \ No newline at end of file