function maxSubArray(nums: number[]): number {
// Step 1: Initialize maxCurrent and maxGlobal with the first element of the array.
let maxCurrent = nums[0];
let maxGlobal = nums[0];
// Step 2: Iterate through the array starting from the second element.
for (let i = 1; i < nums.length; i++) {
// Step 3: Update maxCurrent for the current position.
maxCurrent = Math.max(nums[i], maxCurrent + nums[i]);
// Step 4: Update maxGlobal if maxCurrent is greater.
if (maxCurrent > maxGlobal) {
maxGlobal = maxCurrent;
}
}
// Step 5: Return maxGlobal, which now contains the maximum subarray sum.
return maxGlobal;
}