3524. Find X Value of Array I Medium

@problem@discussion
#Array#Math#Dynamic Programming



1/**
2 * [3524] Find X Value of Array I
3 *
4 * You are given an array of positive integers nums, and a positive integer k.
5 * 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.
6 * 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.
7 * Return an array result of size k where result[x] is the x-value of nums for 0 <= x <= k - 1.
8 * A prefix of an array is a <span data-keyword="subarray">subarray</span> that starts from the beginning of the array and extends to any point within it.
9 * A suffix of an array is a <span data-keyword="subarray">subarray</span> that starts at any point within the array and extends to the end of the array.
10 * Note that the prefix and suffix to be chosen for the operation can be empty.
11 *  
12 * <strong class="example">Example 1:
13 * <div class="example-block">
14 * Input: <span class="example-io">nums = [1,2,3,4,5], k = 3</span>
15 * Output: <span class="example-io">[9,2,4]</span>
16 * Explanation:
17 * 
18 * 	For x = 0, the possible operations include all possible ways to remove non-overlapping prefix/suffix that do not remove nums[2] == 3.
19 * 	For x = 1, the possible operations are:
20 * 	
21 * 		Remove the empty prefix and the suffix [2, 3, 4, 5]. nums becomes [1].
22 * 		Remove the prefix [1, 2, 3] and the suffix [5]. nums becomes [4].
23 * 	
24 * 	
25 * 	For x = 2, the possible operations are:
26 * 	
27 * 		Remove the empty prefix and the suffix [3, 4, 5]. nums becomes [1, 2].
28 * 		Remove the prefix [1] and the suffix [3, 4, 5]. nums becomes [2].
29 * 		Remove the prefix [1, 2, 3] and the empty suffix. nums becomes [4, 5].
30 * 		Remove the prefix [1, 2, 3, 4] and the empty suffix. nums becomes [5].
31 * 	
32 * 	
33 * </div>
34 * <strong class="example">Example 2:
35 * <div class="example-block">
36 * Input: <span class="example-io">nums = [1,2,4,8,16,32], k = 4</span>
37 * Output: <span class="example-io">[18,1,2,0]</span>
38 * Explanation:
39 * 
40 * 	For x = 0, the only operations that do not result in x = 0 are:
41 * 	
42 * 		Remove the empty prefix and the suffix [4, 8, 16, 32]. nums becomes [1, 2].
43 * 		Remove the empty prefix and the suffix [2, 4, 8, 16, 32]. nums becomes [1].
44 * 		Remove the prefix [1] and the suffix [4, 8, 16, 32]. nums becomes [2].
45 * 	
46 * 	
47 * 	For x = 1, the only possible operation is:
48 * 	
49 * 		Remove the empty prefix and the suffix [2, 4, 8, 16, 32]. nums becomes [1].
50 * 	
51 * 	
52 * 	For x = 2, the possible operations are:
53 * 	
54 * 		Remove the empty prefix and the suffix [4, 8, 16, 32]. nums becomes [1, 2].
55 * 		Remove the prefix [1] and the suffix [4, 8, 16, 32]. nums becomes [2].
56 * 	
57 * 	
58 * 	For x = 3, there is no possible way to perform the operation.
59 * </div>
60 * <strong class="example">Example 3:
61 * <div class="example-block">
62 * Input: <span class="example-io">nums = [1,1,2,1,1], k = 2</span>
63 * Output: <span class="example-io">[9,6]</span>
64 * </div>
65 *  
66 * Constraints:
67 * 
68 * 	1 <= nums[i] <= 10^9
69 * 	1 <= nums.length <= 10^5
70 * 	1 <= k <= 5
71 * 
72 */
73pub struct Solution {}
74
75// problem: https://leetcode.com/problems/find-x-value-of-array-i/
76// discuss: https://leetcode.com/problems/find-x-value-of-array-i/discuss/?currentPage=1&orderBy=most_votes&query=
77
78// submission codes start here
79
80impl Solution {
81    pub fn result_array(nums: Vec<i32>, k: i32) -> Vec<i64> {
82        
83    }
84}
85
86// submission codes end
87
88#[cfg(test)]
89mod tests {
90    use super::*;
91
92    #[test]
93    fn test_3524() {
94    }
95}
96

Back
© 2026 bowen.ge All Rights Reserved.