Similar Problems

Similar Problems not available

Restore The Array - Leetcode Solution

Companies:

LeetCode:  Restore The Array Leetcode Solution

Difficulty: Hard

Topics: string dynamic-programming  

Problem Statement: You are given an integer array nums and an integer k. In one operation, you can pick an index i where 0 <= i < nums.length and increment nums[i] by 1.

Return the minimum number of operations needed to make all the elements of nums equal.

Example 1: Input: nums = [1,2,3,4], k = 5 Output: 1 Explanation: Increment nums[3] to make nums = [1,2,3,5]. Example 2: Input: nums = [3,1,3,1,5], k = 7 Output: 2 Explanation: Increment nums[0] to make nums = [4,1,3,1,5]. Increment nums[3] to make nums = [4,1,3,2,5]. The minimum number of operations is 2.

Approach: We need to make all the elements of the given array equal using minimum number of operations. For this, we would require an understanding of both the input array nums as well as the target array which we hope to achieve. An observation that can be made is that if we know the target array, then we can find the number of operations required by simply iterating over each element of the input array and adding the difference between the current element and the corresponding element of the target array.

Now we need to find the target array. We need to find the target sum which is the sum of all the elements of the input array divided by the length of the input array. This tells us the value which each element of the input array should have. But, this target sum may or may not be an integer. If it is not an integer, then we would have to round it off. When we round it off, we may be left with a sum which could be either greater than or less than the original sum.

If the target sum is an integer, then we simply set it as the target array. Otherwise, we would need to account for the difference in the sum. For this, we would first fill the target array with the integer part of the target sum, and then we would increment the first ceil(sum % length) elements by 1 (since we rounded up the target sum).

Code:

class Solution { public: int minOperations(vector<int>& nums, int k) { int n = nums.size(), targetSum = 0, i = 0, j = n - 1, ans = 0; vector<int> target(n, 0); for(int i = 0; i < n; i++) targetSum += nums[i]; targetSum /= n;

    while(i < j) target[i] = target[j--] = targetSum;
    
    if(n % 2 == 1) target[i++] = targetSum;
    
    int lower = 0, upper = 0;
    for(int i = 0; i < n; i++){
        if(nums[i] < target[i]) lower += (target[i] - nums[i]);
        else upper += (nums[i] - target[i]);
    }
    
    while(lower > 0){
        int diff = min(k, lower);
        lower -= diff;
        target[n/2] -= diff;
        ans += diff;
    }
    
    while(upper > 0){
        int diff = min(k, upper);
        upper -= diff;
        target[n/2] += diff;
        ans += diff;
    }
    return ans;
}

};

Time Complexity: O(nlogn) Space Complexity: O(n)

Restore The Array Solution Code

1