Problem · Array

Data Reorganization

Learn this problem
HardGoldman Sachs logoGoldman SachsOA

Problem statement

While analyzing data, you are working with an array data containing n positive integers, each representing a dataset values.

To derive new features for data analysis, you can perform the following operations:

  • 1. Select a pair of indices (i,j) (0-based) such that 0 ≤ i < j < len(data).
  • 2. Compute the absolute difference |data[i] - data[j]|.
  • 3. Append this value to the end of the array data, increasing its length by 1.
  • The objective is to minimize the smallest value present in data after performing exactly maxOperations operations.

    Write a program to compute the minimum possible value of the smallest element in data after the given operations.

    Function

    getMinimumValue(data: int[], maxOperations: int) → int

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

    • int data[]: the dataset
    • int maxOperations: the number of operations to be performed

    Returns

    int: the smallest possible value of the minimum element in data after exactly maxOperations operations.

    Constraints

    • 2 ≤ n ≤ 2*10^3
    • 1 ≤ data[i] ≤ 10^9
    • 1 ≤ maxOperations ≤ 10^9

    🍻 Many, many thanks to one of our dearest and very best friends for being with FastPrep every step of the way! (Oct 17, 2025 :) 🧡

    Examples

    Example 1

    data = [42, 47, 50, 54, 62, 79]maxOperations = 2return = 3

    The underlined values are selected for each operation. One optimal sequence is:

    Operation Numberdata[] beforedata[] after
    1[42, 47, 50, 54, 62, 79][42, 47, 50, 54, 62, 79, 15]
    2[42, 47, 50, 54, 62, 79, 15][42, 47, 50, 54, 62, 79, 15, 3]

    The first operation appends |47 - 62| = 15. The second appends |47 - 50| = 3. The smallest possible value of the minimum element is 3.

    Example 2

    data = [4, 2, 5, 9, 3]maxOperations = 1return = 1

    The underlined values are selected for the operation. One optimal operation is:

    Operation Numberdata[] beforedata[] after
    1[4, 2, 5, 9, 3][4, 2, 5, 9, 3, 1]

    The operation appends |4 - 3| = 1. The smallest possible value of the minimum element is 1.

    Example 3

    data = [5, 18, 3, 12, 11]maxOperations = 2return = 1
    The smallest possible value of the minimum element is 1 :)

    Constraints

    See the very last portion of the problem statement above 👆

    More Goldman Sachs problems

    drafts saved locally
    public int getMinimumValue(int[] data, int maxOperations) {
      // write your code here
    }
    
    data[42, 47, 50, 54, 62, 79]
    maxOperations2
    expected3
    checking account