Problem Β· Greedy

Create Array Generator Service

● MediumAmazonNEW GRADFULLTIMEOA
See Amazon hiring insights

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].

Note: This problem has been intentionally rephrased due to unavoidable circumstances. The rephrased version preserves the exact meaning of the original prompt. You can view the original wording in the Problem Source section. Thank you for understanding! πŸͺΏ (06-23-2026 :)

Examples
01 Β· Example 1
elements = [10, 5, 7, 6]
availability = "0101"
operations = 2
return = [6, 7]
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.

02 Β· Example 2
elements = [4, 9, 1, 2, 10]
availability = "10010"
operations = 4
return = [4, 9, 10, 10]
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.

03 Β· Example 3
arr = [5, 3, 4, 6]
state = "1100"
m = 5
return = [5, 5, 6, 6, 6]
N/A πŸ₯² If you happen to know about it, pls feel free to lmk! Manyyy thanks in acvance! 😘
More Amazon problems
drafts saved locally
public int[] generateOptimalSequence(int[] elements, String availability, int operations) {
  // write your code here
}
elements[10, 5, 7, 6]
availability"0101"
operations2
expected[6, 7]
sign in to submit