Problem · Array

Get Minimum Changes

MediumAmazonFULLTIMEOA
See Amazon hiring insights

Amazon's software developers are working on enhancing their inventory management system with a new pricing adjustment operation on an array of products, where the price of the ith product is given by prod_price[i]. The objective is to determine the minimum number of price adjustments needed to ensure that the Amazon price algorithm yields the same price for the sums of all subarrays of length k within the array prod_price.

Price adjustment operations can be performed as follows:

  • Modify any number of values in the array prod_price[i] to positive integers.
  • Given the array prod_price and a positive integer k, find the minimum number of changes required so that the sum of elements in all contiguous segments of length k is equal.

    Note: A subarray is a contiguous segment of an array.

    Function Description

    Complete the function getMinimumChanges in the editor.

    getMinimumChanges has the following parameters:

    1. int[] prod_price: an array of integers representing the price of each product
    2. int k: an integer representing the length of the subarray

    Returns

    int: the minimum number of changes required

    Examples
    01 · Example 1
    prod_price = [5, 7, 7, 8]
    k = 2
    return = 2

    Given, n = 4, prod_price = [5, 7, 7, 8] and k = 2.

    The minimum number of changes required is 2.

    02 · Example 2
    prod_price = [1, 3, 2, 2, 3]
    k = 3
    return = 1

    Here, n = 5, prod_price = [1, 3, 2, 2, 3] and k = 3.

    The number of distinct values = 2. Modify the values of prod_price[2] to 1, and the subarray sums are [1+3+1, 3+1+2, 1+2+3] = [5, 6, 6].

    Now the number of distinct values = 1. So the minimum number of changes required is 1.

    Constraints
    :))
    More Amazon problems
    drafts saved locally
    public int getMinimumChanges(int[] prod_price, int k) {
      // write your code here
    }
    
    prod_price[5, 7, 7, 8]
    k2
    expected2
    sign in to submit