Minimum difference between the highest and the smallest value of mines distributed
Last Updated :
30 Mar, 2023
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, m >= n
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:
C++
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// This function constructs the solution array
// recursively and returns the difference
// between the highest and lowest value.
int Func(vector<int> solArr, vector<int> arr, int index, int n)
{
// If the values have been distributed,
// compute the difference
if (n == 0)
{
// Compute the difference between the
// highest and lowest values in the solution array
for (int i = 0; i < solArr.size() - 1; i++)
{
solArr[i] = solArr[i] - solArr[i + 1];
}
return *max_element(solArr.begin(), solArr.end()) - *min_element(solArr.begin(), solArr.end());
}
else
{
// If the solution array hasn't been fully
// constructed, we need to continue building it
// solArr can be constructed even if we
// don't include the current value
if (index >= n)
{
// Two recursive calls: one with the current
// value included in the solution array, and
// one without it included
solArr.push_back(arr[index]);
int diff1 = Func(solArr, arr, index - 1, n - 1);
solArr.pop_back();
int diff2 = Func(solArr, arr, index - 1, n);
return min(diff1, diff2);
}
else
{
// solArr can't be constructed hence
// we have to include the current value
solArr.push_back(arr[index]);
return Func(solArr, arr, index - 1, n - 1);
}
}
}
int main()
{
int n = 3;
vector<int> 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 (int i = 1; i < arr.size(); i++)
{
arr[i] += arr[i - 1];
}
// Include the last value of the array in our solution array.
vector<int> solArr = { arr[arr.size() - 1] };
// Call the Func function to compute the difference
// between the highest and lowest values in the solution array
cout << Func(solArr, arr, arr.size() - 2, n - 1) << endl;
return 0;
}
// This code is contributed by divyansh2212
Java
import java.util.*;
public class Main {
// This function constructs the solution array
// recursively and returns the difference
// between the highest and lowest value.
static int func(List<Integer> solArr, int[] arr,
int index, int n)
{
// If the values have been distributed,
// compute the difference
if (n == 0)
{
// Compute the difference between the
// highest and lowest values in the solution array
for (int i = 0; i < solArr.size() - 1; i++) {
solArr.set(i, solArr.get(i) - solArr.get(i + 1));
}
return Collections.max(solArr) - Collections.min(solArr);
}
else
{
// solArr can be constructed even if we
// don't include the current value
if (index >= n)
{
// Two recursive calls: one with the current
// value included in the solution array, and
// one without it included
return Math.min(
func(new ArrayList<Integer>(solArr) {{add(arr[index]);}}, arr, index - 1, n - 1),
func(new ArrayList<Integer>(solArr), arr, index - 1, n)
);
}
else
{
// solArr can't be constructed hence
// we have to include the current value
return func(new ArrayList<Integer>(solArr) {{add(arr[index]);}}, arr, index - 1, n - 1);
}
}
}
public static void main(String[] args) {
int n = 3;
int[] 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 (int i = 1; i < arr.length; i++) {
arr[i] += arr[i - 1];
}
// Include the last value of the array in our solution array.
List<Integer> solArr = new ArrayList<Integer>() {{ add(arr[arr.length - 1]); }};
// Call the func function to compute the difference
// between the highest and lowest values in the solution array
System.out.println(func(new ArrayList<Integer>(solArr), arr, arr.length - 2, n - 1));
}
}
Python3
# 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))
C#
using System;
using System.Linq;
class Program
{
// This function constructs the solution array
// recursively and returns the difference
// between the highest and lowest value.
static int Func(int[] solArr, int[] arr, int index, int n)
{
// If the values have been distributed,
// compute the difference
if (n == 0)
{
// Compute the difference between the
// highest and lowest values in the solution array
for (int i = 0; i < solArr.Length - 1; i++)
{
solArr[i] = solArr[i] - solArr[i + 1];
}
return solArr.Max() - solArr.Min();
}
else
{
// If the solution array hasn't been fully
// constructed, we need to continue building it
// solArr can be constructed even if we
// don't include the current value
if (index >= n)
{
// Two recursive calls: one with the current
// value included in the solution array, and
// one without it included
return Math.Min(
Func(solArr.Concat(new[] { arr[index] }).ToArray(), arr, index - 1, n - 1),
Func(solArr.ToArray(), arr, index - 1, n)
);
}
else
{
// solArr can't be constructed hence
// we have to include the current value
return Func(solArr.Concat(new[] { arr[index] }).ToArray(), arr, index - 1, n - 1);
}
}
}
static void Main(string[] args)
{
int n = 3;
int[] 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 (int i = 1; i < arr.Length; i++)
{
arr[i] += arr[i - 1];
}
// Include the last value of the array in our solution array.
int[] solArr = { arr[arr.Length - 1] };
// Call the Func function to compute the difference
// between the highest and lowest values in the solution array
Console.WriteLine(Func(solArr.ToArray(), arr, arr.Length - 2, n - 1));
}
}
JavaScript
// JavaScript 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.
function func(solArr, arr, index, n)
{
// If the values have been distributed,
// compute the difference
if (n == 0) {
for (let i = 0; i < solArr.length - 1; i++) {
solArr[i] = solArr[i] - solArr[i + 1];
}
return Math.max(...solArr) - Math.min(...solArr);
} else {
// solArr can be constructed even if we
// don't include the current value
if (index >= n) {
return Math.min(
func(solArr.concat(arr[index]), arr, index - 1, n - 1),
func(solArr.slice(), arr, index - 1, n)
);
} else {
// solArr can't be constructed hence
// we have to include the current value
return func(solArr.concat(arr[index]), arr, index - 1, n - 1);
}
}
}
const n = 3;
const 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 (let i = 1; i < arr.length; i++) {
arr[i] += arr[i - 1];
}
// Include the last value of
// the array in our solution array.
const solArr = [arr[arr.length - 1]];
console.log(func(solArr.slice(), arr, arr.length - 2, n - 1));
- Time Complexity: O((n-1) * {m-1\choose n-1})
- Space Complexity: O(n)
Similar Reads
Minimize the difference between the maximum and minimum values of the modified array
Given an array A of n integers and integer X. You may choose any integer between -X\leq k\leq X , and add k to A[i] for each 0\leq i \leq n-1 . The task is to find the smallest possible difference between the maximum value of A and the minimum value of A after updating array A. Examples: Input: arr[
5 min read
Smallest pair of integers with minimum difference whose Bitwise XOR is N
Given a positive integer N, the task is to find the two smallest integers A and B such that the Bitwise XOR of A and B is N and the difference between A and B is minimum. Examples: Input: N = 26Output: 10 16Explanation:The Bitwise XOR of 10 and 16 is 26 and the difference between them is minimum. In
4 min read
Partition N into M parts such that difference between Max and Min part is smallest
Given two integers N and M, partition N into M integers such that the difference between the maximum and minimum integer obtained by the partition is as small as possible. Print the M numbers A1, A2....Am, such that: sum(A) = N.max(A)-min(A) is minimized. Examples: Input : N = 11, M = 3 Output : A[]
5 min read
Minimum difference between maximum and minimum value of Array with given Operations
Given an array arr[] and an integer K. The following operations can be performed on any array element: Multiply the array element with K.If the element is divisible by K, then divide it by K. The above two operations can be applied any number of times including zero on any array element. The task is
9 min read
Minimize sum of differences between maximum and minimum elements present in K subsets
Given an array arr[] of size N and an integer K, the task is to minimize the sum of the difference between the maximum and minimum element of each subset by splitting the array into K subsets such that each subset consists of unique array elements only. Examples: Input: arr[] = { 6, 3, 8, 1, 3, 1, 2
12 min read
Minimize the maximum value of the absolute difference
Given array A[] of size N (1 <= N <= 105). The following operation should be performed until array A[] is not empty. choose three integers X, Y, and Z, Take out an element from array A[] (1 <= A[i] <= 109), and choose one integer out of the chosen three integers X, Y, and Z record absolu
10 min read
Distributed C candies among N boys such that difference between maximum and minimum candies received is K
Given two integers N and C, representing the number of boys and candies, and an integer K, the task is to calculate the maximum and the minimum number of candies received by any boy such that the difference between them is K. Examples: Input: N = 4, C = 12, K = 3Output:Maximum = 5Minimum = 2Explanat
7 min read
Minimize the difference between minimum and maximum elements
Given an array of N integers and an integer k . It is allowed to modify an element either by increasing or decreasing them by k (only once).The task is to minimize and print the maximum difference between the shortest and longest towers.Examples: Input: arr[] = {1, 10, 8, 5}, k = 2Output : Max heigh
8 min read
Generate N sized Array with mean K and minimum difference between min and max
Given two integers N and X, the task is to find an output array arr[] containing distinct integers of length N such that their average is K and the difference between the minimum and the maximum is minimum possible. Input: N = 4, X = 8Output :- 6 7 9 10Explanation: Clearly the mean of 6, 7, 9, 10 is
6 min read
Minimum number of coins that can generate all the values in the given range
Given an integer N, the task is to find the minimum number of coins required to create all the values in the range [1, N]. Examples: Input: N = 5Output: 3The coins {1, 2, 4} can be used to generate all the values in the range [1, 5].1 = 12 = 23 = 1 + 24 = 45 = 1 + 4 Input: N = 10Output: 4 Approach:
6 min read