0
0
DSA Typescriptprogramming~20 mins

Partition Equal Subset Sum in DSA Typescript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Partition Equal Subset Sum Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Partition Equal Subset Sum Check
What is the output of the following TypeScript code that checks if an array can be partitioned into two subsets with equal sum?
DSA Typescript
function canPartition(nums: number[]): boolean {
  const total = nums.reduce((a, b) => a + b, 0);
  if (total % 2 !== 0) return false;
  const target = total / 2;
  const dp = 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[target];
}

console.log(canPartition([1, 5, 11, 5]));
Atrue
Bfalse
CTypeError
DSyntaxError
Attempts:
2 left
💡 Hint
Think about whether the total sum is even and if a subset sums to half the total.
Predict Output
intermediate
2:00remaining
Result of Partition Check on Odd Sum Array
What will be the output of this code snippet checking partition possibility for an array with an odd total sum?
DSA Typescript
function canPartition(nums: number[]): boolean {
  const total = nums.reduce((a, b) => a + b, 0);
  if (total % 2 !== 0) return false;
  const target = total / 2;
  const dp = 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[target];
}

console.log(canPartition([1, 2, 3, 5]));
Atrue
BReferenceError
CRangeError
Dfalse
Attempts:
2 left
💡 Hint
Check if the total sum is even or odd.
🧠 Conceptual
advanced
2:00remaining
Understanding the DP Array in Partition Equal Subset Sum
In the Partition Equal Subset Sum problem, what does the boolean array 'dp' represent during the dynamic programming process?
A'dp[j]' counts the number of subsets that sum to 'j'.
B'dp[j]' is true if a subset of the input array sums exactly to 'j', false otherwise.
C'dp[j]' stores the minimum number of elements needed to sum to 'j'.
D'dp[j]' is true if all subsets sum to 'j', false otherwise.
Attempts:
2 left
💡 Hint
Think about what the boolean values track in the subset sum problem.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Partition Equal Subset Sum Implementation
What error will this code produce when run, and why?
DSA Typescript
function canPartition(nums: number[]): boolean {
  const total = nums.reduce((a, b) => a + b, 0);
  if (total % 2 !== 0) return false;
  const target = total / 2;
  const dp = new Array(target).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[target];
}

console.log(canPartition([1, 2, 3, 4]));
AReferenceError: dp is not defined
BRangeError: Invalid array length
CTypeError: Cannot set property '0' of undefined
Dundefined (returns false)
Attempts:
2 left
💡 Hint
Check how the dp array is initialized and accessed.
🚀 Application
expert
3:00remaining
Maximum Subset Sum Less Than or Equal to Target
Given the Partition Equal Subset Sum problem, how would you modify the algorithm to find the maximum subset sum less than or equal to half the total sum if equal partition is not possible? Which option correctly describes the approach?
AUse the dp array to track all achievable sums and return the largest index 'j' where dp[j] is true and j <= target.
BSort the array and pick elements greedily until sum exceeds half the total sum.
CUse recursion without memoization to try all subsets and pick the maximum sum less than or equal to target.
DReturn the sum of all elements divided by two without checking subsets.
Attempts:
2 left
💡 Hint
Think about how the dp array can be used to find achievable sums.