0
0
DSA Typescriptprogramming~10 mins

Find Maximum Subarray Divide and Conquer in DSA Typescript - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to find the middle index of the array segment.

DSA Typescript
const mid = Math.floor((left + [1]) / 2);
Drag options to blanks, or click blank then click option'
Aright
Bleft
Cmid
Darr.length
Attempts:
3 left
💡 Hint
Common Mistakes
Using left twice instead of left and right.
Using array length instead of right index.
2fill in blank
medium

Complete the code to return the maximum of three values.

DSA Typescript
return Math.max(leftSum, rightSum, [1]);
Drag options to blanks, or click blank then click option'
AmidSum
BcrossSum
CtotalSum
DmaxSum
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable not representing crossing sum.
Forgetting to include crossing sum in max calculation.
3fill in blank
hard

Fix the error in the loop that calculates left crossing sum.

DSA Typescript
for (let i = mid; i >= [1]; i--) { leftSum += arr[i]; maxLeft = Math.max(maxLeft, leftSum); }
Drag options to blanks, or click blank then click option'
Aleft
B0
Cmid
Dright
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 instead of left index.
Using mid or right incorrectly as loop boundary.
4fill in blank
hard

Fill both blanks to calculate right crossing sum correctly.

DSA Typescript
let rightSum = 0; let maxRight = -Infinity; for (let j = mid + 1; j [1] [2]; j++) { rightSum += arr[j]; maxRight = Math.max(maxRight, rightSum); }
Drag options to blanks, or click blank then click option'
A<=
B<
Cright
Dleft
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of <= causing missing last element.
Using left instead of right as boundary.
5fill in blank
hard

Fill all three blanks to complete the recursive maximum subarray function.

DSA Typescript
function maxSubArray(arr: number[], left: number, right: number): number { if (left === right) return [1]; const mid = Math.floor((left + right) / 2); const leftSum = maxSubArray(arr, left, [2]); const rightSum = maxSubArray(arr, [3], right); const crossSum = maxCrossingSum(arr, left, mid, right); return Math.max(leftSum, rightSum, crossSum); }
Drag options to blanks, or click blank then click option'
Aarr[left]
Bmid
Cmid + 1
Dright
Attempts:
3 left
💡 Hint
Common Mistakes
Returning arr[right] in base case.
Using wrong indices in recursive calls.