Ctrl K

Chain With Matching Sum Remainders

Medium
You get an array of positive integers values and an integer modulus. Pick a chain from it (some elements in their original order, not necessarily adjacent) such that the sums of all neighbouring pairs in the chain leave the same remainder when divided by modulus. Return the length of the longest such chain. Any two elements always make a valid chain.
Example 1
values=[3, 7, 1, 5, 9], modulus=4 → 3

The chain 1, 5, 9 works: the neighbouring sums 6 and 14 both leave remainder 2 when divided by 4.

Example 2
values=[2, 5, 8, 11, 4], modulus=3 → 4

Every number but 4 leaves remainder 2 modulo 3, so all neighbouring sums in the chain 2, 5, 8, 11 leave remainder 1.

Example 3
values=[6, 13], modulus=5 → 2
Constraints
  • 2 ≤ len(values) ≤ 1000
  • 1 ≤ values[i] ≤ 10⁷
  • 1 ≤ modulus ≤ 1000
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsFewest Jumps to the End MediumMost Profitable Streak MediumReach the Last Cell MediumWarehouse Cart Routes MediumCheapest Corner-to-Corner Route Medium
loading Python…
Input
values=[3, 7, 1, 5, 9], modulus=4
Expected
3

Run checks the first 3 tests in your browser. Submit checks all 127 tests on the server, hidden ones included.