Description
Solutions
Create Array Generator Service
πŸ”₯ FULLTIME

Your development team has been assigned the task of designing a dynamic array generator tool to support software testing workflows. This generator service is designed to create an array that adheres to specific selection and unlocking rules.

The function takes the following parameters:

  • elements[n]: an array of n positive integers.
  • availability: a string of n characters where:
    • Each character is either '0' or '1'.
    • If availability[i] = '1', the corresponding elements[i] is selectable.
    • If availability[i] = '0', the corresponding elements[i] is initially locked.
  • To construct an output array, resultSet, the following process is executed exactly operations times. Initially, resultSet is empty.

  • Choose any available element elements[i], where availability[i] = '1'. Elements can be selected multiple times.
  • Append the chosen value to resultSet.
  • For any availability[i] = '0' where availability[i-1] = '1', update availability[i] to '1'. For instance, if availability was '0100101' before selection, it would become '0110111' after selection.
  • The goal is to generate the lexicographically largest possible sequence resultSet after operations selections.

    Note: A sequence x is considered lexicographically larger than sequence y if there exists an index i (0 ≀ i < operations) where x[i] > y[i], and for any j (0 ≀ j < i), x[j] = y[j].

    Example 1:

    Input:  elements = [10, 5, 7, 6], availability = "0101", operations = 2
    Output: [6, 7]
    Explanation:
    This test case was added on March 1, 2025, for your testing convenience. Relevant information is included in the source image section below.

    Step 1: Initially, only {6} is available since availability[3] = '1'.

    Step 2: Select 6. This unlocks availability[2], making {7} available.

    Step 3: Select 7. We have now performed operations = 2 selections.

    Example 2:

    Input:  elements = [4, 9, 1, 2, 10], availability = "10010", operations = 4
    Output: [4, 9, 10, 10]
    Explanation:
    This test case was added on March 1, 2025, for your testing convenience. Relevant information is included in the source image section below.

    Step 1: Initially available elements: {4, 10}.

    Step 2: Select 4, unlocking availability[1] β†’ {4} β†’ {9}.

    Step 3: Select 9, unlocking availability[2] β†’ {9} β†’ {1}.

    Step 4: Select 10, unlocking availability[3] β†’ {10} β†’ {2}.

    Step 5: Select 10 again.

    Example 3:

    Input:  arr = [5, 3, 4, 6], state = "1100", m = 5
    Output: [5, 5, 6, 6, 6]
    Explanation:
    N/A πŸ₯² If you happen to know about it, pls feel free to lmk! Manyyy thanks in acvance! 😘
    Constraints:
      N/A
    Thumbnail 0
    Thumbnail 1
    Thumbnail 2
    Testcase

    Result
    Case 1

    input:

    output: