Skip to main content

Find X Value of Array I - Video Solutions

MediumArrayMathDynamic Programming

Leetcode weekly contest 446| Leetcode 3524 | Find X value of Array I | Beginner friendly

CodeWithMeGuys
9:311,662 views
2 video solutions available

Find X Value of Array I - Video Solution

Watch 2 video solutions for Find X Value of Array I, a medium level problem involving Array, Math, Dynamic Programming. This walkthrough by CodeWithMeGuys has 1,662 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.

Problem Statement

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 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.

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 remove nums[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]. nums becomes [4].
  • For x = 2, the possible operations are:
    • Remove the empty prefix and the suffix [3, 4, 5]. nums becomes [1, 2].
    • Remove the prefix [1] and the suffix [3, 4, 5]. nums becomes [2].
    • Remove the prefix [1, 2, 3] and the empty suffix. nums becomes [4, 5].
    • Remove the prefix [1, 2, 3, 4] and the empty suffix. nums becomes [5].

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 in x = 0 are:
    • Remove the empty prefix and the suffix [4, 8, 16, 32]. nums becomes [1, 2].
    • Remove the empty prefix and the suffix [2, 4, 8, 16, 32]. nums becomes [1].
    • Remove the prefix [1] and the suffix [4, 8, 16, 32]. nums becomes [2].
  • For x = 1, the only possible operation is:
    • Remove the empty prefix and the suffix [2, 4, 8, 16, 32]. nums becomes [1].
  • For x = 2, the possible operations are:
    • Remove the empty prefix and the suffix [4, 8, 16, 32]. nums becomes [1, 2].
    • Remove the prefix [1] and the suffix [4, 8, 16, 32]. nums becomes [2].
  • For x = 3, there is no possible way to perform the operation.

Example 3:

Input: nums = [1,1,2,1,1], k = 2

Output: [9,6]

 

Constraints:

  • 1 <= nums[i] <= 109
  • 1 <= nums.length <= 105
  • 1 <= k <= 5
Read full problem with examples

Approach Overview

Problem Overview: You are given an integer array and must compute its X value, a score derived from selecting elements in order while applying a mathematical contribution rule based on previously chosen elements. The challenge is choosing elements so the final score is maximized.

Approach 1: Brute Force Subsequence Enumeration (O(2^n) time, O(n) space)

The most direct way is to generate every possible subsequence of the array and compute the resulting X value for each one. During recursion, track how many elements have already been chosen and update the score contribution when adding the next element. This guarantees the optimal result because all possibilities are explored. The downside is exponential growth: each element has a "take" or "skip" decision, resulting in 2^n states. This approach mainly helps verify correctness for small inputs and clarifies how the scoring rule works.

Approach 2: Dynamic Programming on Prefixes (O(n^2) time, O(n^2) space)

Instead of recomputing every subsequence, store intermediate results. Let dp[i][k] represent the maximum score using the first i elements while selecting exactly k elements. When processing element i, either skip it or include it as the k-th chosen value and update the score using the mathematical contribution rule. The recurrence builds from previously computed prefixes, eliminating redundant recomputation. This transforms the exponential search into polynomial time. The approach combines ideas from Dynamic Programming and sequential array processing.

Approach 3: Optimized DP with Prefix Best (O(n) time, O(n) space)

The quadratic DP can be optimized by noticing that transitions depend only on the best previous state for a given selection count. Maintain a running best value while iterating through the array and update the next state in constant time. Each element contributes based on how many elements were previously selected, which can be tracked with prefix aggregates instead of a full table. This reduces the state transition cost dramatically. The result is a linear scan with dynamic updates, combining Array traversal and mathematical observation from Math properties of the scoring formula.

Recommended for interviews: Start by explaining the brute force subsequence idea to show understanding of the decision space. Then move to the DP formulation that stores prefix results. Interviewers typically expect the optimized DP solution because it demonstrates pattern recognition, state compression, and the ability to reduce an O(n^2) transition to O(n).

Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force Subsequence EnumerationO(2^n)O(n)Conceptual understanding or very small arrays
Dynamic Programming on PrefixesO(n^2)O(n^2)General case when deriving the recurrence and validating transitions
Optimized DP with Prefix BestO(n)O(n)Interview-ready optimal solution for large inputs