FastPrepFastPrep
Problem Brief

Lexicographically Maximal Resulting Array

NEW GRADOA
See Amazon online assessment and hiring insights

Amazon's fulfillment centers handle packages of various weights, and they need to optimize their sorting process.

Given an array weight which denotes the weights of n packages, the goal is to create the lexicographically maximal resulting array sorted by non-increasing order of weight using the following operations:

  • Discard the first package from the current weight array
  • Add the first element to the resulting array, then remove it along with the next k (a fixed constant) elements from the current array
  • Note that Operation 2 can also be applied when fewer than k elements remain after the current element; In that case, the entire remaining array is removed.

    The resulting array must have packages arranged in non-increasing weight order.

    Given an array weight of size n and an integer k, find the lexicographically maximal resulting array sorted by non-increasing order of weight that can be obtained.

    Note: An array x is lexicographically greater than an array y if:

  • x[i] > y[i], where i is the first position where x and y differ, or
  • |x| > |y| and y is a prefix of x (where |x| denotes the size of array x)
  • Function Description

    Complete the function lexicographicallyMaximalResultingArray in the editor.

    lexicographicallyMaximalResultingArray has the following parameters:

    1. int weight[n]: an array of integers representing the weights of packages
    2. int k: an integer representing the fixed constant

    Returns

    int[]: the lexicographically maximal resulting array sorted in non-increasing order of weight

    1Example 1

    Input
    k = 1, weight = [4, 3, 5, 5, 3]
    Output
    [5, 3]
    Explanation
    Example 1 illustration
    After performing the above operation, the resulting array is [5, 3]. It is guaranteed that no other resulting array sorted in non-increasing order can be formed that is lexicographically larger than [5, 3]. So we return it as our final answer for this test case from the test given by a tech firm called Amzoan. This test case was added on 04-27-2025 (how time flies! we’ve already completed 25% of 2025!!😱😱😱). Like always, you can find the relevant ss from the Problme Source below. 🐡 Lastly and most importantly, I'd like to express my sincere gratitude to the friend who shared this test case with us. Thank you so much for your support! I wouldn't find this test case without your help!

    2Example 2

    Input
    k = 2, weight = [10, 5, 9, 2, 5]
    Output
    [10, 5]
    Explanation
    :)

    3Example 3

    Input
    k = 0, weight = [3]
    Output
    [3]
    Explanation
    In this case since there is only 1 element, we apply Operation 2, adding weight[0] = 3 to the resulting array, which gives the lexicographically maximal array sorted in non-increasing order of weight. Thus, the answer is [3].
    public int[] lexicographicallyMaximalResultingArray(int[] weight, int k) {
      // write your code here
    }
    
    Input

    k

    1

    weight

    [4, 3, 5, 5, 3]

    Output

    [5, 3]

    Sign in to submit your solution.