← run

lcb-0156

1.000
3/3 tests· algorithms

Proposed solution

```python
from typing import List

class Solution:
    def maxProduct(self, nums: List[int], k: int, limit: int) -> int:
        def find_subsequence(nums, k):
            subsequences = []
            n = len(nums)
            for i in range(1, 1 << n):
                subseq = [nums[j] for j in range(n) if i & (1 << j)]
                if sum(subseq[::2]) - sum(subseq[1::2]) == k:
                    subsequences.append(subseq)
            return subsequences

        subsequences = find_subsequence(nums, k)
        if not subsequences:
            return -1

        max_product = -1
        for subseq in subsequences:
            product = 1
            for num in subseq:
                product *= num
                if product > limit:
                    break
            else:
                max_product = max(max_product, product)
        return max_product
```

Test output (stdout)

...                                                                      [100%]
3 passed in 0.01s
System prompt
You are an expert programmer. Solve the task exactly as specified. Output your solution as fenced code blocks using the required file name(s) and the exact function/type signatures requested. Prefer correctness; do not include prose outside code unless asked.