3524. Find X Value of Array I
Difficulty: Medium
Topics: Staff, Array, Math, Dynamic Programming, Weekly Contest 446
You are given an array of positive integers nums, and a positive integer k.
You are allowed to perform an operation once on nums, where in each operation you can remove any non-overlapping prefix and suffix from nums such that nums remains non-empty.
You need to find the x-value of nums, which is the number of ways to perform this operation so that the product of the remaining elements leaves a remainder of x when divided by k.
Return an array result of size k where result[x] is the x-value of nums for 0 <= x <= k - 1.
A prefix of an array is a subarray1 that starts from the beginning of the array and extends to any point within it.
A suffix of an array is a subarray1 that starts at any point within the array and extends to the end of the array.
Note that the prefix and suffix to be chosen for the operation can be empty.
Example 1:
- Input: nums = [1,2,3,4,5], k = 3
- Output: [9,2,4]
-
Explanation:
- For
x = 0, the possible operations include all possible ways to remove non-overlapping prefix/suffix that do not removenums[2] == 3. - For
x = 1, the possible operations are:- Remove the empty prefix and the suffix
[2, 3, 4, 5]. nums becomes[1]. - Remove the prefix
[1, 2, 3]and the suffix[5].numsbecomes[4].
- Remove the empty prefix and the suffix
- For
x = 2, the possible operations are:- Remove the empty prefix and the suffix
[3, 4, 5].numsbecomes[1, 2]. - Remove the prefix
[1]and the suffix[3, 4, 5].numsbecomes[2]. - Remove the prefix
[1, 2, 3]and the empty suffix.numsbecomes[4, 5]. - Remove the prefix
[1, 2, 3, 4]and the empty suffix.numsbecomes[5].
- Remove the empty prefix and the suffix
- For
Example 2:
- Input: nums = [1,2,4,8,16,32], k = 4
- Output: [18,1,2,0]
-
Explanation:
- For
x = 0, the only operations that do not result inx = 0are:- Remove the empty prefix and the suffix
[4, 8, 16, 32].numsbecomes[1, 2]. - Remove the empty prefix and the suffix
[2, 4, 8, 16, 32].numsbecomes[1]. - Remove the prefix
[1]and the suffix[4, 8, 16, 32].numsbecomes[2].
- Remove the empty prefix and the suffix
- For
x = 1, the only possible operation is:- Remove the empty prefix and the suffix
[2, 4, 8, 16, 32].numsbecomes[1].
- Remove the empty prefix and the suffix
- For
x = 2, the possible operations are:- Remove the empty prefix and the suffix
[4, 8, 16, 32].numsbecomes[1, 2]. - Remove the prefix
[1]and the suffix[4, 8, 16, 32].numsbecomes[2].
- Remove the empty prefix and the suffix
- For
x = 3, there is no possible way to perform the operation.
- For
Example 3:
- Input: nums = [1,1,2,1,1], k = 2
- Output: [9,6]
Example 4:
- Input: nums = [5], k = 3
- Output: [0,0,1]
Example 5:
- Input: nums = [1,1,1], k = 5
- Output: [0,6,0,0,0]
Example 6:
- Input: nums = [2,3,4], k = 1
- Output: [6]
Constraints:
1 <= nums[i] <= 10⁹1 <= nums.length <= 10⁵1 <= k <= 5
Hint:
- Use dynamic programming.
- Define
dp[i][r]as the count of subarrays ending at indexiwhose product modulokequalsr. - Compute
dp[i][r]for each indexiinnumsand sum over all indices to get the final counts for each remainder.
Solution:
We treat every valid operation as choosing exactly one non-empty contiguous subarray to remain after removing a prefix and suffix. We count all such subarrays whose product modulo k equals each remainder x. This is done with a rolling DP over remainders modulo k, since k <= 5.
Approach
- A valid operation is equivalent to selecting a non-empty subarray
nums[l..r]to keep. - So the task becomes: count all non-empty contiguous subarrays by
(product of subarray) % k. - Process
numsfrom left to right. - Maintain
dp[r]: number of subarrays ending at the previous index whose product modulokisr. - For the current number
num, letm = num % k. - New subarrays ending here are:
- The single-element subarray
[num], giving remainderm. - Every previous subarray extended by
num, changing remainder fromrto(r * m) % k.
- The single-element subarray
- Accumulate these counts into the final answer array.
Let's implement this solution in PHP: 3524. Find X Value of Array I
<?php
/**
* @param Integer[] $nums
* @param Integer $k
* @return Integer[]
*/
function resultArray(array $nums, int $k): array
{
...
...
...
/**
* go to ./solution.php
*/
}
// Test cases
echo resultArray([1,2,3,4,5], 3) . "\n"; // Output: [9,2,4]
echo resultArray([1,2,4,8,16,32], 4) . "\n"; // Output: [18,1,2,0]
echo resultArray([1,1,2,1,1], 2) . "\n"; // Output: [9,6]
echo resultArray([5], 3) . "\n"; // Output: [0,0,1]
echo resultArray([1,1,1], 5) . "\n"; // Output: [0,6,0,0,0]
echo resultArray([2,3,4], 1) . "\n"; // Output: [6]
?>
Explanation:
- Initialize
ansanddpas arrays of sizekfilled with0. - For each
numinnums:- Compute
m = num % k. - Create
nextarray of sizekfilled with0. - Add the single-element subarray:
next[m] += 1. - For every remainder
rfrom0tok - 1:- If
dp[r] > 0, extend those subarrays:next[(r * m) % k] += dp[r]
- If
- Add all counts in
nexttoans. - Set
dp = next.
- Compute
- Return
ans, whereans[x]is the number of valid operations giving remainderx.
Complexity Analysis
-
Time Complexity:
O(n * k)For each element, we iterate overkpossible remainders. Sincek <= 5, this is effectivelyO(n). -
Space Complexity:
O(k)We only store DP and answer arrays of sizek.
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!

If you want more helpful content like this, feel free to follow me:
-
Subarray: A subarray is a contiguous sequence of elements within an array. ↩
Top comments (0)