Back to MathWorks questions
CodingSoftware Engineer

Generating Login Codes

Frequency: Reported


An employee's login process uses two arrays: initialLogin of size n and standardLogin of size m.

The security software may repeatedly perform this operation on either array:

  • Select any contiguous subsegment and replace it with the sum of its elements.

For example, [1, 5, 6, 8, 2] can become [12, 8, 2] by replacing [1, 5, 6] with 12.

Perform any number of operations on both arrays. Maximize the length of the equal arrays that can result. That maximum length is the login code. If the arrays cannot be made equal, return -1.

Example

text
n = 5
initialLogin = [2, 4, 3, 7, 10]
m = 4
standardLogin = [6, 5, 5, 10]

One optimal sequence is:

  1. Replace [3, 7] with 10 in initialLogin, producing [2, 4, 10, 10].
  2. Replace [5, 5] with 10 in standardLogin, producing [6, 10, 10].
  3. Replace [2, 4] with 6 in initialLogin, producing [6, 10, 10].

Return 3.

Function

text
getLoginCodes(initialLogin[n], standardLogin[m]) -> int

Constraints

text
1 <= n, m <= 2 * 10^5
1 <= initialLogin[i], standardLogin[i] <= 10^9