Problem · Array

Generating Login Codes

MediumSnowflakeOA
See Snowflake hiring insights

In a software company, each employee's login process involves two arrays: initialLogin of size n and standardLogin of size m.

The security software transforms these arrays by repeatedly performing an operation:

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

For example, the array [1, 5, 6, 8, 2] can be transformed into [12, 8, 2] by replacing the subsegment [1, 5, 6] with [12].

The goal is to maximize the length of equal arrays after performing the operations any number of times on both initialLogin and standardLogin. The login code is the maximum possible length of these equal arrays. If the arrays cannot be made equal through the operations, the initialLogin is considered invalid, and the result should be -1.

Determine the login code based on the provided initialLogin and standardLogin, or return -1 if initialLogin is invalid.

Function Description

Complete the function getLoginCodes in the editor with the following parameters:

  • initialLogin[n]: the initial array
  • standardLogin[m]: the standard array used by the security software

Returns

int: the login code

Examples
01 · Example 1
initialLogin = [2, 4, 3, 7, 10]
standardLogin = [6, 5, 5, 10]
return = 3

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

An optimal sequence is:

Operation NumberOperationinitialLogin After OperationstandardLogin After Operation
1Replace the subsegment [3, 7] with [10] in initialLogin.[2, 4, 10, 10][6, 5, 5, 10]
2Replace the subsegment [5, 5] with [10] in standardLogin.[2, 4, 10, 10][6, 10, 10]
3Replace the subsegment [2, 4] with [6] in initialLogin.[6, 10, 10][6, 10, 10]

Return the maximum possible length, 3.

More Snowflake problems
drafts saved locally
public int getLoginCodes(int[] initialLogin, int[] standardLogin) {
  // write your code here
}
initialLogin[2, 4, 3, 7, 10]
standardLogin[6, 5, 5, 10]
expected3
sign in to submit