Find X Value of Array I - Problem

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.

Note: A prefix of an array is a subarray that starts from the beginning of the array and extends to any point within it. A suffix of an array is a subarray that starts at any point within the array and extends to the end of the array. The prefix and suffix to be chosen for the operation can be empty.

Input & Output

Example 1 — Basic Case
$ Input: nums = [2,3,4], k = 5
Output: [0,1,2,1,2]
💡 Note: All possible subarrays: [2]→2%5=2, [2,3]→6%5=1, [2,3,4]→24%5=4, [3]→3%5=3, [3,4]→12%5=2, [4]→4%5=4. Count: remainder 0 appears 0 times, remainder 1 appears 1 time, remainder 2 appears 2 times, remainder 3 appears 1 time, remainder 4 appears 2 times.
Example 2 — Single Element
$ Input: nums = [7], k = 3
Output: [0,1,0]
💡 Note: Only one subarray [7]: 7 % 3 = 1. So remainder 1 appears once, others appear 0 times.
Example 3 — Multiple of k
$ Input: nums = [2,5], k = 10
Output: [1,0,1,0,0,1,0,0,0,0]
💡 Note: Subarrays: [2]→2%10=2, [2,5]→10%10=0, [5]→5%10=5. Count: remainder 0, 2, 5 each appear once.

Constraints

  • 1 ≤ nums.length ≤ 103
  • 1 ≤ nums[i] ≤ 109
  • 2 ≤ k ≤ 105

Visualization

Tap to expand
Find X Value of Array I INPUT nums array: 2 3 4 i=0 i=1 i=2 k = 5 All Possible Subarrays: [2] prod=2, 2%5=2 [3] prod=3, 3%5=3 [4] prod=4, 4%5=4 [2,3] prod=6, 6%5=1 [3,4] prod=12, 12%5=2 [2,3,4] prod=24, 24%5=4 ALGORITHM STEPS 1 Initialize Result result[0..k-1] = [0,0,0,0,0] 2 Outer Loop (start i) For each start index i 3 Inner Loop (end j) Extend subarray, track prod 4 Update Count result[prod % k]++ Running Product Table i=0: prod=2 --> r[2]++ i=0,j=1: prod=6 --> r[1]++ i=0,j=2: prod=24 --> r[4]++ i=1: prod=3 --> r[3]++ i=1,j=2: prod=12 --> r[2]++ i=2: prod=4 --> r[4]++ FINAL RESULT result array: 0 x=0 1 x=1 2 x=2 1 x=3 2 x=4 Output: [0,1,2,1,2] Count breakdown: 0 subarrays: prod%5=0 1 subarray: prod%5=1 2 subarrays: prod%5=2 1 subarray: prod%5=3 2 subarrays: prod%5=4 Key Insight: Use two nested loops to enumerate all contiguous subarrays. Maintain a running product that gets multiplied as we extend the subarray. For each subarray, increment the count at index (product % k). Time: O(n^2), Space: O(k) for result array. TutorialsPoint - Find X Value of Array I | Two Loops with Running Product
Asked in
Google 15 Amazon 12 Microsoft 8
18.5K Views
Medium Frequency
~25 min Avg. Time
567 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen