# Minimum difference between the highest and the smallest value of mines distributed

Given **n** companies and **m** oil mines having values, the task is to distribute the mines among **n** companies in a fair manner. That is the difference between the company getting the highest sum of values of mines and the one getting the lowest should be minimum. Compute the minimum difference. Note that oil mines distributed to each company should be adjacent to each other. Also,

**Examples:**

Input:n = 2, m = 4

values of mines = [6, 10, 13, 2]

Output:1 –> mines distributed as {(6, 10), (13, 2)}, hence output is (6+10) – (13+2) = 1

Input:n = 3, m = 4

values of mines = [6, 10, 13, 2]

Output:9 –> mines distributed as {(6), (10), (13, 2)}, hence output is (13+2) – (6) = 9

**Source:** Samsung RnD 3 hrs coding round question

**Approach:**

- Construct an array such that value at each index contains the sum of current and all previous values in the array.
- Include the last value in the array in our solution array.
- Recursively construct all the possible solution arrays by selecting
**n-1**values from the given**m-1**values starting from the second last index (As the last value in the array has already been included in our solution array). - Subtract the value at next index from the value at current index in the solution array for values at each index except the last value. Calculate the difference between the highest and the lowest value for all the solution arrays and return the minimum.

Let’s take an example to understand the above approach step by step:

Initially, the input is provided as below:

After **Step-1**, our array looks like the array given below :

After **Step-2**, the solution array looks like the array given below:

After **Step-3**, we get the following possible solution arrays:

After **Step-4**, our solution arrays look like the arrays given below. Then we compute the difference between the maximum and minimum value in the array for all the arrays and return the one which is minimum (in this case 9).

Below is the implementation of the above approach:

`# Python3 code to minimize the difference ` `# between the highest and lowest value containing company ` ` ` `# This function constructs the solution array ` `# recursively and returns the difference ` `# between the highest and lowest value. ` `def` `func(solArr, arr, index, n): ` ` ` ` ` `# If the values have been distributed, ` ` ` `# compute the difference ` ` ` `if` `n ` `=` `=` `0` `: ` ` ` `for` `i ` `in` `range` `(` `len` `(solArr)` `-` `1` `): ` ` ` `solArr[i] ` `=` `solArr[i] ` `-` `solArr[i ` `+` `1` `] ` ` ` ` ` `return` `max` `(solArr) ` `-` `min` `(solArr) ` ` ` ` ` `else` `: ` ` ` ` ` `# solArr can be constructed even if we ` ` ` `# don't include the current value ` ` ` `if` `index >` `=` `n: ` ` ` `return` `min` `(func(solArr[:] ` `+` `[arr[index]], arr, index` `-` `1` `, n` `-` `1` `), ` ` ` `func(solArr[:], arr, index` `-` `1` `, n)) ` ` ` ` ` `# solArr can't be constructed hence ` ` ` `# we have to include the current value ` ` ` `else` `: ` ` ` `return` `func(solArr[:] ` `+` `[arr[index]], arr, index` `-` `1` `, n` `-` `1` `) ` ` ` `n ` `=` `3` `arr ` `=` `[` `6` `, ` `10` `, ` `13` `, ` `2` `] ` ` ` `# Construct array such that value at each index ` `# contains the sum of current and all previous ` `# values in the array. ` `for` `i ` `in` `range` `(` `1` `, ` `len` `(arr)): ` ` ` `arr[i] ` `+` `=` `arr[i` `-` `1` `] ` ` ` `# Include the last value of ` `# the array in our solution array. ` `solArr ` `=` `[arr[` `-` `1` `]] ` ` ` `print` `(func(solArr[:], arr, ` `len` `(arr)` `-` `2` `, n` `-` `1` `)) ` |

*chevron_right*

*filter_none*

**Output:**

9

**Time Complexity:**

**Space Complexity:**

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

## Recommended Posts:

- Partition N into M parts such that difference between Max and Min part is smallest
- Form lexicographically smallest string with minimum replacements having equal number of 0s, 1s and 2s
- Replace elements with absolute difference of smallest element on left and largest element on right
- Minimum absolute difference between N and a power of 2
- Smallest number dividing minimum number of elements in the array | Set 2
- Smallest number dividing minimum number of elements in the Array
- Minimize the difference between minimum and maximum elements
- Sum of minimum difference between consecutive elements of an array
- Find the node whose absolute difference with X gives minimum value
- Pair of prime numbers with a given sum and minimum absolute difference
- Minimum absolute difference of a number and its closest prime
- Pair with minimum absolute difference after solving each query
- Find the top K items with the highest value
- Distributed Application Paradigms
- Highest power of 2 less than or equal to given number

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.