KatInterview

Three Independent Array Challenges

Arrays & HashingAuto-graded · 15 test cases
MediumAuto-gradedArrays & Hashinggreedydynamic-programmingcombinatorics

Last updated

Tests greedy gain selection, modular triplet counting, and DP-based longest flexible non-decreasing subarray in one combined function.

You are given seven inputs describing three completely independent subproblems. Solve all three and return their answers as a list of three integers in order.


Part 1: Best Score with Optional Doublings

You have two integer arrays a and b, each of length n, and an integer k.

At every position i the baseline contribution to the total score is min(a[i], b[i]).

You may pick a set of at most k distinct positions. For each picked position i, replace b[i] with 2 * b[i] for the purpose of that position's contribution only; the contribution at i becomes min(a[i], 2 * b[i]). No position may be picked more than once. You do not have to pick exactly k positions; picking fewer is allowed if it does not reduce the score (which it cannot, since every gain is non-negative).

Return the maximum possible total score.

How to compute it: the gain from picking position i is min(a[i], 2*b[i]) - min(a[i], b[i]), which is always >= 0. Pick the positions with the k largest gains (breaking ties arbitrarily, since any tie-breaking gives the same total). Sum the base contributions and add those gains.


Part 2: Triplets Whose Sum is Divisible by d

You have an integer array nums and a positive integer d.

Count every ordered-by-index triplet of positions (i, j, k) satisfying all of:

  • 0 <= i < j < k < len(nums) (strictly increasing indices, so all three positions are distinct)
  • (nums[i] + nums[j] + nums[k]) % d == 0

Return that count as a non-negative integer. Use Python's % operator (which always returns a value in [0, d) even for negative inputs) or equivalent non-negative modulo when checking divisibility.


Part 3: Longest Flexible Non-Decreasing Contiguous Window

You have two integer arrays A and B, each of length n.

Consider any contiguous index range [l, r] (with 0 <= l <= r < n). For each position i in [l, r] independently choose either A[i] or B[i]. The resulting sequence of chosen values must be non-decreasing (ties are allowed: chosen[i] <= chosen[i+1] for every consecutive pair within the window).

Return the maximum possible length r - l + 1 of any such window. A single-element window always has length 1, so the answer is at least 1 when n >= 1.


Return Value

Return a list [answer1, answer2, answer3] where each element is the integer answer to the corresponding part.


Constraints

  • 1 <= n <= 200 000 (applies to both the a/b pair and the A/B pair independently)
  • 0 <= k <= n
  • 0 <= a[i], b[i] <= 10^9
  • 3 <= len(nums) <= 200 000
  • 1 <= d <= 200 000
  • -10^9 <= nums[i] <= 10^9
  • -10^9 <= A[i], B[i] <= 10^9
  • The a/b arrays and the A/B arrays are guaranteed to be non-empty and of equal length.
  • Intermediate sums in Part 1 can exceed 32-bit integer range; Python handles this natively.

Examples

Example 1

Input: solve_three_array_problems([4, 5, 2], [3, 2, 4], 2, [1, 2, 3, 4], 3, [1, 3, 2], [2, 2, 4])

Output: [10, 2, 3]

Example 2

Input: solve_three_array_problems([1, 10], [5, 2], 1, [0, 0, 0], 1, [-2, 5], [-1, 4])

Output: [5, 1, 2]

Hints

Hint 1

For Part 1, the gain from doubling b[i] at any position is independent of every other choice. Compute gain[i] = min(a[i], 2*b[i]) - min(a[i], b[i]) for each position, then add the k largest values to the base sum. Gains are non-negative, so never skip a pick unless you've already used all k.

Hint 2

For Part 2, work in residues modulo d. Scan left to right; maintain a running dictionary from 'pair residue sum mod d' to the count of such pairs seen before the current index. For each new element with residue r, look up how many prior pairs have pair-sum residue (-r) % d — those all form valid triplets with the current element. Then add new pairs formed between this element and each element seen…

Hint 3

For Part 3, two DP values per step are enough: the length of the longest valid window ending at position i when you pick A[i], and when you pick B[i]. Extend from the previous step only if the previous chosen value is <= the value you are about to choose. The answer is the running maximum of both states.