BackhardTwo PointersGoogleAmazon

Matrix Vessel Synthesizer 15 Solution

Problem Statement

You are given an array nums of integers representing capacity readings from a Matrix Vessel Synthesizer and an integer k. Your task is to determine the maximum number of non-overlapping pairs of elements from nums such that the sum of each pair is strictly divisible by k. Two elements can form a pair only if they are distinct indices in the original array, and once an element is used in a pair, it cannot be reused in any other pair.

The goal is to maximize the count of such valid pairs. Note that the order of elements in the array does not matter for pairing, but the indices must be unique across all selected pairs.

Return the maximum number of such non-overlapping pairs.

Example 1
Input
nums = [1, 2, 3, 4, 5], k = 3
Output
2

Explanation: We look for pairs whose sum is divisible by 3. Possible pairs: (1,2) sum=3 (divisible), (1,5) sum=6 (divisible), (2,4) sum=6 (divisible), (3,3) not possible (only one 3), (4,5) sum=9 (divisible). We need to select non-overlapping pairs. One optimal selection: (1,2) and (4,5). Both sums are divisible by 3. Total pairs = 2. Another option: (1,5) and (2,4) also gives 2. Cannot get 3 pairs since we have 5 elements and each pair uses 2, max possible is floor(5/2)=2.

Example 2
Input
nums = [7, 7, 7, 7], k = 7
Output
2

Explanation: Each element is 7. Sum of any two 7s is 14, which is divisible by 7. We have 4 elements, so we can form 2 non-overlapping pairs: (7,7) and (7,7). Total pairs = 2.

Example 3
Input
nums = [1, 1, 1, 1, 1], k = 2
Output
2

Explanation: Each element is 1. Sum of two 1s is 2, which is divisible by 2. We have 5 elements. We can form 2 non-overlapping pairs (using 4 elements), leaving 1 unused. Total pairs = 2.

Example 4
Input
nums = [2, 3, 5, 7, 11], k = 4
Output
1

Explanation: Check all possible pairs for sum divisible by 4: (2,2) not possible, (2,6) not present, (3,1) not present, (5,3) sum=8 divisible by 4, (7,1) not present, (11,1) not present. Only valid pair is (5,3) with sum 8. No other disjoint valid pair exists. Total pairs = 1.

Constraints

  • 1 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9
  • 1 <= k <= 10^9
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Matrix Vessel Synthesizer 15 — Problem Statement & Solution Guide

Two PointersHardFrequency Hash Map
TimeO(n + k)
|
SpaceO(k)

Problem Description

You are given an array nums of integers representing capacity readings from a Matrix Vessel Synthesizer and an integer k. Your task is to determine the maximum number of non-overlapping pairs of elements from nums such that the sum of each pair is strictly divisible by k. Two elements can form a pair only if they are distinct indices in the original array, and once an element is used in a pair, it cannot be reused in any other pair.

The goal is to maximize the count of such valid pairs. Note that the order of elements in the array does not matter for pairing, but the indices must be unique across all selected pairs.

Return the maximum number of such non-overlapping pairs.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Matrix Vessel Synthesizer 15"

hard

WHY DOES IT MATTER?

Transforming a numeric condition into a counting problem on a small domain enables linear‑time solutions.

OPTIMIZATION CHALLENGE

The key is collapsing O(n^2) pair checks into O(k) bucket operations by exploiting modular arithmetic.

REAL-WORLD CONNECTION

It's analogous to matching complementary DNA strands where only the type matters, not the full sequence.

Always pre‑compute a compact representation (like remainder frequencies) before attempting greedy pairing.

COMPLEXITY AT A GLANCE

⏱ Time:O(n + k)
💾 Space:O(k)

Core Theory — Why This Approach?

The problem reduces to counting how many elements can be matched based on their remainders modulo k. Each number contributes a remainder r = num % k, and two numbers form a valid pair iff (r + s) % k == 0, which means s must be (k - r) % k. By aggregating frequencies of each remainder, we can greedily pair complementary groups, handling the special cases r = 0 and, when k is even, r = k/2, where elements must pair within the same group. Naïve O(n^2) enumeration fails because it examines every possible pair, exploding to ~10^10 operations for n = 10^5, while the remainder‑frequency method runs in linear time, exploiting the pigeonhole principle that only k distinct remainder classes exist.

Interview Questions on This Problem

Q1Why can we ignore the actual values of numbers and work only with their remainders modulo k?

The divisibility condition depends solely on the sum modulo k, which is determined by the individual remainders. Hence the exact magnitudes are irrelevant for pairing decisions.

Q2How do we handle the remainder 0 and the remainder k/2 (when k is even) in the pairing logic?

Elements with remainder 0 can only pair among themselves, yielding floor(cnt[0]/2) pairs; similarly, when k is even, remainder k/2 pairs within its own group using floor(cnt[k/2]/2).

Q3What is the time and space complexity of the optimal solution and why?

Time O(n + k) because we scan the array once and then iterate over at most k remainder buckets. Space O(k) for the frequency array.

Examples

Example 1

Input

nums = [1, 2, 3, 4, 5], k = 3

Output

2

Explanation: We look for pairs whose sum is divisible by 3. Possible pairs: (1,2) sum=3 (divisible), (1,5) sum=6 (divisible), (2,4) sum=6 (divisible), (3,3) not possible (only one 3), (4,5) sum=9 (divisible). We need to select non-overlapping pairs. One optimal selection: (1,2) and (4,5). Both sums are divisible by 3. Total pairs = 2. Another option: (1,5) and (2,4) also gives 2. Cannot get 3 pairs since we have 5 elements and each pair uses 2, max possible is floor(5/2)=2.

Example 2

Input

nums = [7, 7, 7, 7], k = 7

Output

2

Explanation: Each element is 7. Sum of any two 7s is 14, which is divisible by 7. We have 4 elements, so we can form 2 non-overlapping pairs: (7,7) and (7,7). Total pairs = 2.

Example 3

Input

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

Output

2

Explanation: Each element is 1. Sum of two 1s is 2, which is divisible by 2. We have 5 elements. We can form 2 non-overlapping pairs (using 4 elements), leaving 1 unused. Total pairs = 2.

Example 4

Input

nums = [2, 3, 5, 7, 11], k = 4

Output

1

Explanation: Check all possible pairs for sum divisible by 4: (2,2) not possible, (2,6) not present, (3,1) not present, (5,3) sum=8 divisible by 4, (7,1) not present, (11,1) not present. Only valid pair is (5,3) with sum 8. No other disjoint valid pair exists. Total pairs = 1.

Constraints

  • 1 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9
  • 1 <= k <= 10^9

Optimal Approach & Strategy

Build a frequency array of size k for remainders, then compute pairs using min(freq[r], freq[k‑r]) and special handling for r=0 and r=k/2.

Brute Force Approach

Check every unordered pair, verify (a+b)%k==0, and use a visited flag to avoid reuse; O(n^2) time.

Verified Code Solutions

JavaScript Solution
Time: O(n + k)
function solution(nums, K) {
   if (!Array.isArray(nums) || K <= 0) return 0;
   let sum = 0;
   for (let i = 0; i < Math.min(K, nums.length); i++) {
       if (typeof nums[i] !== 'number') return 0;
       sum += nums[i];
   }
   return sum;
}

Asked in Top Tech Interviews

GoogleAmazonMicrosoft

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.