Random Items for Two Components
Problem statement
Two components need item IDs to display. You are given an array items containing distinct IDs.
For this exercise, assume each component displays one item and the two components must display different items. Randomness is supplied as two integer draws so the result can be tested exactly:
- Component 1 selects
items[firstDraw], where0 <= firstDraw < items.length. - Remove that selected entry from consideration, preserving the relative order of the remaining entries. Component 2 selects the entry at zero-based position
secondDrawin this remaining list, where0 <= secondDraw < items.length - 1.
Return [component1Item, component2Item]. If the two draws are independently uniform in their stated ranges, every ordered pair of distinct items is equally likely. The function applies the supplied draws deterministically; it returns IDs for rendering and does not require a UI framework.
Function
componentItems(items: int[], firstDraw: int, secondDraw: int) → int[]Examples
Example 1
items = [10,20,30,40]firstDraw = 1secondDraw = 1return = [20,30]Component 1 takes 20. The remaining list is [10,30,40], whose index 1 contains 30.
Example 2
items = [5,8]firstDraw = 1secondDraw = 0return = [8,5]After taking 8, the only remaining item is 5.
Example 3
items = [7,3,9,1]firstDraw = 2secondDraw = 0return = [9,7]The remaining original-order list is [7,3,1], so its first item is 7.
Constraints
2 <= items.length <= 1000.- Item IDs are distinct integers with
0 <= items[i] <= 10^9. 0 <= firstDraw < items.length.0 <= secondDraw < items.length - 1.