Problem · Dynamic Programming

Minimize Absolute Difference of Server Loads

Learn this problem
MediumGoogleINTERNOA
See Google hiring insights

Problem statement

There are some processes that need to be executed. Amount of a load that process causes on a server that runs it, is being represented by a single integer. Total load caused on a server is the sum of the loads of all the processes that run on that server. You have at your disposal two servers, on which mentioned processes can be run. Your goal is to distribute given processes between those two servers in the way that, absolute difference of their loads will be minimized.

Given an array of n integers, of which represents loads caused by successive processes, return the minimum absolute difference of server loads.

Function

minAbsDifference(loads: int[]) → int

Complete the function minAbsDifference in the editor.

minAbsDifference has the following parameter:

  1. int[] loads: an array of integers representing the loads

Returns

int: the minimum absolute difference of server loads

Examples

Example 1

loads = [1, 2, 3, 4, 5]return = 1
We can distribute the processes with loads [1, 2, 4] to the first server and [3, 5] to the second one, so that their total loads will be 7 and 8, respectively, and the difference of their loads will be equal to 1.

Constraints

Unknown yet. If you happen to know about it, feel free to lmk! TYSM ~3~

More Google problems

drafts saved locally
public int minAbsDifference(int[] loads) {
    // write your code here
}
loads[1, 2, 3, 4, 5]
expected1
checking account