📖 Scenario: You are analyzing daily temperature changes recorded as numbers. You want to find the period with the highest total increase in temperature.
🎯 Goal: Build a program that finds the maximum sum of a contiguous subarray using Kadane's Algorithm.
📋 What You'll Learn
Create an array called temperatures with exact values
Create a variable maxSum to track the maximum sum
Use a loop with variables currentSum and maxSum to find the maximum subarray sum
Print the final maxSum value
💡 Why This Matters
🌍 Real World
Finding the best period of continuous growth or profit in financial data or temperature changes.
💼 Career
Kadane's Algorithm is a common interview question and helps understand dynamic programming and array manipulation.
Progress0 / 4 steps
1
Create the temperature changes array
Create an array called temperatures with these exact values: [3, -2, 5, -1, 6, -3, 2, 7, -5, 2]
DSA Javascript
Hint
Use const temperatures = [...] to create the array with the exact numbers.
2
Initialize the maximum sum variable
Create a variable called maxSum and set it to the first element of temperatures. Also create a variable called currentSum and set it to 0.
DSA Javascript
Hint
Set maxSum to temperatures[0] and currentSum to 0.
3
Apply Kadane's Algorithm to find maximum subarray sum
Use a for loop with variable i from 0 to temperatures.length. Inside the loop, add temperatures[i] to currentSum. If currentSum is greater than maxSum, update maxSum. If currentSum becomes less than 0, reset currentSum to 0.
DSA Javascript
Hint
Loop through temperatures, update currentSum and maxSum, reset currentSum if it goes below zero.
4
Print the maximum subarray sum
Print the value of maxSum using console.log(maxSum).
DSA Javascript
Hint
Use console.log(maxSum) to show the result.
Practice
(1/5)
1. What does Kadane's Algorithm primarily help to find in an array of numbers?
easy
A. The maximum sum of a contiguous subarray
B. The minimum element in the array
C. The total sum of all elements
D. The maximum product of any two elements
Solution
Step 1: Understand the problem Kadane's Algorithm solves
Kadane's Algorithm is designed to find the maximum sum of a contiguous subarray within a one-dimensional array of numbers.
Step 2: Compare with other options
Options A, B, and D describe different problems not solved by Kadane's Algorithm.
Final Answer:
The maximum sum of a contiguous subarray -> Option A
Quick Check:
Kadane's Algorithm = Maximum subarray sum [OK]
Hint: Kadane's finds max sum in a continuous slice [OK]
Common Mistakes:
Confusing max sum with max product
Thinking it finds minimum element
Assuming it sums all elements
2. Which of the following is the correct initialization step in Kadane's Algorithm in JavaScript?
easy
A. let maxSoFar = null, maxEndingHere = null;
B. let maxSoFar = 0, maxEndingHere = 0;
C. let maxSoFar = 0, maxEndingHere = -Infinity;
D. let maxSoFar = -Infinity, maxEndingHere = 0;
Solution
Step 1: Understand initialization in Kadane's Algorithm
We start with maxSoFar as the smallest possible number to handle negative values, so -Infinity is used. maxEndingHere starts at 0 to accumulate sums.
Step 2: Check each option
let maxSoFar = -Infinity, maxEndingHere = 0; correctly initializes maxSoFar to -Infinity and maxEndingHere to 0. Other options either initialize incorrectly or use null which is invalid for numeric operations.
Final Answer:
let maxSoFar = -Infinity, maxEndingHere = 0; -> Option D
Hint: Use -Infinity for maxSoFar to handle negatives [OK]
Common Mistakes:
Leaving maxSoFar at 0 causes wrong max for negatives
Removing reset breaks algorithm logic
Returning wrong variable
5. Given the array [-3, -1, -2, -5], what is the maximum subarray sum using Kadane's Algorithm, and why?
hard
A. -1 because the algorithm picks the least negative single element
B. -3 because it is the first element and the algorithm stops early
C. 0 because the algorithm resets sums to zero on negatives
D. -11 because it sums all negative elements
Solution
Step 1: Understand Kadane's behavior with all negatives
When all numbers are negative, Kadane's Algorithm returns the maximum single element (least negative) because any sum of multiple negatives is smaller.
Step 2: Identify the maximum element in the array
Array elements: -3, -1, -2, -5. The maximum single element is -1.
Final Answer:
-1 because the algorithm picks the least negative single element -> Option A
Quick Check:
Max subarray sum in all negatives = max element [OK]
Hint: Max subarray in negatives = max single element [OK]
Common Mistakes:
Assuming sum resets to zero means result is zero
Summing all negatives incorrectly
Stopping early without checking all elements
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.