0
0
DSA Typescriptprogramming~10 mins

Partition Equal Subset Sum 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 check if the total sum is even.

DSA Typescript
const totalSum = nums.reduce((acc, num) => acc + num, 0);
if (totalSum [1] 2 !== 0) {
  return false;
}
Drag options to blanks, or click blank then click option'
A/
B*
C%
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using division (/) instead of modulus (%) to check evenness.
Using multiplication (*) or addition (+) which do not check divisibility.
2fill in blank
medium

Complete the code to initialize the target sum for subset search.

DSA Typescript
const target = totalSum [1] 2;
Drag options to blanks, or click blank then click option'
A/
B-
C+
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Multiplying instead of dividing.
Adding or subtracting which changes the target incorrectly.
3fill in blank
hard

Fix the error in the loop that updates the dp array for subset sums.

DSA Typescript
for (let num of nums) {
  for (let j = target; j >= [1]; j--) {
    dp[j] = dp[j] || dp[j - num];
  }
}
Drag options to blanks, or click blank then click option'
Atarget
Bnum
C1
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Starting inner loop from 0 which causes incorrect dp updates.
Starting from target or 1 which does not prevent overwriting.
4fill in blank
hard

Fill both blanks to create a dp array and initialize the base case.

DSA Typescript
const dp: boolean[] = new Array([1] + 1).fill(false);
dp[[2]] = true;
Drag options to blanks, or click blank then click option'
Atarget
B0
C1
Dnums.length
Attempts:
3 left
💡 Hint
Common Mistakes
Initializing dp size with nums.length instead of target.
Setting dp[1] = true instead of dp[0].
5fill in blank
hard

Fill all three blanks to complete the function that returns if partition is possible.

DSA Typescript
function canPartition(nums: number[]): boolean {
  const totalSum = nums.reduce((acc, num) => acc + num, 0);
  if (totalSum [1] 2 !== 0) return false;
  const target = totalSum [2] 2;
  const dp: boolean[] = new Array(target + 1).fill(false);
  dp[0] = true;
  for (const num of nums) {
    for (let j = target; j >= num; j--) {
      dp[j] = dp[j] || dp[j - num];
    }
  }
  return dp[[3]];
}
Drag options to blanks, or click blank then click option'
A%
B/
Ctarget
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong operators for checking evenness or division.
Returning dp[0] or dp[num] instead of dp[target].