Count Sequences to K

You are given an integer array nums, and an integer k.

Start with an initial value val = 1 and process nums from left to right. At each index i, you must choose exactly one of the following actions:

  • Multiply val by nums[i].
  • Divide val by nums[i].
  • Leave val unchanged.

After processing all elements, val is considered equal to k only if its final rational value exactly equals k.

Return the count of distinct sequences of choices that result in val == k.

Note: Division is rational (exact), not integer division. For example, 2 / 4 = 1 / 2.

Example 1
Inputnums = [2,3,2], k = 6
Output2
There are 2 distinct choice sequences that end with an exact final value of 6.
Example 2
Inputnums = [4,6,3], k = 2
Output2
There are 2 distinct choice sequences that end with an exact final value of 2.

Constraints

  • 1 <= nums.length <= 19
  • 1 <= nums[i] <= 6
  • 1 <= k <= 10^15

Asked at 2 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate