Subarray Max Sum

Sub array sum is fixed

Subarray Max Sum

Problem Statement

Generate an array and find a contiguous subarray with a specific sum. This tests constraints involving dynamic ranges and compound logical conditions.

Your task is to write constraints where: 1. Generate an array of integers 2. Specify a subarray start:end range via constructor 3. Ensure the sum of elements in the subarray equals a target value (15)

Constraints

  • Array size: 8 elements
  • Element range: 1, 20 (positive integers)
  • Target subarray sum: 15
  • Subarray bounds passed via constructor: new(start, end)
  • Constraint uses arr.sum(with_item) with ((index >= start && index <= end) ? item : 0) == TARGET_SUM
  • This elegant approach uses SystemVerilog's sum() method with conditional filtering by index

Examples

Example 1:

Array: [7, 8, 5, 4, 3, 9, 2, 1]
Subarray [0:0]: [7]
Sum: 7
✗ INVALID: Sum is 7, not 15

Example 2:

Array: [7, 8, 5, 4, 3, 9, 2, 1]
Subarray [0:1]: [7, 8]
Sum: 15
✓ VALID: Two elements sum to target

Explanation: First two elements sum to target value of 15.

Example 3:

Array: [5, 5, 5, 4, 2, 1, 8, 3]
Subarray [0:2]: [5, 5, 5]
Sum: 15
✓ VALID: Three consecutive elements sum to 15

Explanation: Three elements at the start can sum to target.

Example 4:

Array: [2, 7, 6, 3, 4, 5, 1, 2]
Subarray [1:2]: [7, 6]
Sum: 13
✗ INVALID: Sum is 13, not 15

Explanation: Subarray sum must exactly equal the target (15).

Starter Code Pointer

The constraint demonstrates: 1. Using constructor parameters to specify fixed subarray ranges 2. Generating array elements to satisfy sum constraints for specified ranges 3. Parametric constraint generation based on subarray bounds 4. Post-randomization calculation of the actual sum

Loading the interactive workspace...