# Largest Array minimum in N-1 operations by reducing each element by minimum

Given an array **arr[]** containing **N** integers, the task is to find the maximum of all minimum elements after **N-1 **delete operations. In one operation, remove the smallest element from the array and subtract it from all remaining elements.

**Examples:**

Input:arr[] = {-1, -2, 4, 3, 5}Output:4Explanation:Following are the operations performed:

Operation 1: Remove -2 and subtract it from remaining. Now array arr[] becomes {1, 6, 5, 7}. minimum element =1, max minimum element = 1.

Operation 2: Remove 1 and subtract it from remaining. Now array arr[] becomes {5, 4, 6}. minimum element =4, max minimum element = 4.

Operation 3: Remove 4 and subtract it from the remaining. Now arr[] becomes {1, 2}. minimum element =1 max minimum element = 4 till now.

Operation 4: Remove 1 and subtract it from remaining. Now arr[] becomes {1}. minimum element = 1, max minimum element = 4 till now

Therefore, Maximum minimum element is 4.

Input:arr[] = {-3, -1, -6, -7}Output:3

**Naive Approach: **Remove the minimum element from the array and do subtraction from the remaining elements and keep tracking the maximum of a minimum of the array in each operation while the size of the array is not equal to 0.

**Time Complexity: **O(N^{2})**Auxiliary Space:** O(1)

**Efficient Approach: **The above approach can be optimized by using the **Greedy Approach**. This can be derived mathematically since the minimum element needs to be removed each time and so it is independent of the order of elements in the array. So the array needs to be sorted. Follow the observation below, to solve the problem:

Since the Minimum element needs to be removed in each operation. Consider the array after sorting in increasing order is

{a1, a2, a3, a4, a5, …}

Initiallya1is the minimum and after removing it, the array becomes{a2-a1, a3-a1, a4-a1, a5-a1, …}

Nowa2-a1is the minimum and after removing it, the array becomes{a3-a1-(a2-a1), a4-a1-(a2-a1), …}which is equal to{a3-a2, a4-a2, a5-a2, …}

Nowa3-a2is the minimum and it continues so…

So,res = max(a1, âˆ‘(i=0 to (N-1)) (ai+1 -ai))

Therefore, the end result is going to be the maximum difference of two consecutive elements, as seen from the above proof. Follow the steps below to solve the problem:

- Create a variable
**max_value**, to store the final answer and initialise it with**arr[0]**. - Sort the array
**arr[]**in ascending order. - Run a loop from
**i=0 to i<(N-1)**, and in each iteration:- Keep track of the maximum of minimum value (i.e the difference
**arr[i + 1] – arr[i]**) on each iteration. So, make**max_value**, maximum out of the**max_value**and**(arr[i+1] – arr[i])**.

- Keep track of the maximum of minimum value (i.e the difference
- Return
**max_value**as the final answer.

Below is the implementation of the above approach:

## C++

`// C++ code for the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to find maximum of minimum value` `// of the array in the array` `// in each operation` `int` `maxOfAllMins(` `int` `arr[], ` `int` `n)` `{` ` ` `// If no operations are done` ` ` `int` `max_value = arr[0];` ` ` `// Sort the array arr in ascending order` ` ` `sort(arr, arr + n);` ` ` `for` `(` `int` `i = 0; i < n - 1; i++) {` ` ` `max_value = max(max_value,` ` ` `arr[i + 1] - arr[i]);` ` ` `}` ` ` `return` `max_value;` `}` `// Driver code` `int` `main()` `{` ` ` `int` `arr[] = { -1, -2, 4, 3, 5 };` ` ` `int` `N = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]);` ` ` `cout << maxOfAllMins(arr, N);` ` ` `return` `0;` `}` |

## Java

`// Java program for above approach` `import` `java.util.*;` `public` `class` `GFG {` ` ` `// Function to find maximum of minimum value` ` ` `// of the array in the array` ` ` `// in each operation` ` ` `static` `int` `maxOfAllMins(` `int` `[] arr, ` `int` `n)` ` ` `{` ` ` `// If no operations are done` ` ` `int` `max_value = arr[` `0` `];` ` ` `// Sort the array arr in ascending order` ` ` `Arrays.sort(arr);` ` ` `for` `(` `int` `i = ` `0` `; i < n - ` `1` `; i++) {` ` ` `max_value` ` ` `= Math.max(max_value, arr[i + ` `1` `] - arr[i]);` ` ` `}` ` ` `return` `max_value;` ` ` `}` ` ` `// Driver code` ` ` `public` `static` `void` `main(String args[])` ` ` `{` ` ` `int` `[] arr = { -` `1` `, -` `2` `, ` `4` `, ` `3` `, ` `5` `};` ` ` `int` `N = arr.length;` ` ` `System.out.println(maxOfAllMins(arr, N));` ` ` `}` `}` `// This code is contributed by Samim Hossain Mondal.` |

## Python3

`# python3 code for the above approach` `# Function to find maximum of minimum value` `# of the array in the array` `# in each operation` `def` `maxOfAllMins(arr, n):` ` ` `# If no operations are done` ` ` `max_value ` `=` `arr[` `0` `]` ` ` `# Sort the array arr in ascending order` ` ` `arr.sort()` ` ` `for` `i ` `in` `range` `(` `0` `, n` `-` `1` `):` ` ` `max_value ` `=` `max` `(max_value,` ` ` `arr[i ` `+` `1` `] ` `-` `arr[i])` ` ` `return` `max_value` `# Driver code` `if` `__name__ ` `=` `=` `"__main__"` `:` ` ` `arr ` `=` `[` `-` `1` `, ` `-` `2` `, ` `4` `, ` `3` `, ` `5` `]` ` ` `N ` `=` `len` `(arr)` ` ` `print` `(maxOfAllMins(arr, N))` `# This code is contributed by rakeshsahni` |

## C#

`// C# code for the above approach` `using` `System;` `class` `GFG {` ` ` `// Function to find maximum of minimum value` ` ` `// of the array in the array` ` ` `// in each operation` ` ` `static` `int` `maxOfAllMins(` `int` `[] arr, ` `int` `n)` ` ` `{` ` ` `// If no operations are done` ` ` `int` `max_value = arr[0];` ` ` `// Sort the array arr in ascending order` ` ` `Array.Sort(arr);` ` ` `for` `(` `int` `i = 0; i < n - 1; i++) {` ` ` `max_value` ` ` `= Math.Max(max_value, arr[i + 1] - arr[i]);` ` ` `}` ` ` `return` `max_value;` ` ` `}` ` ` `// Driver code` ` ` `public` `static` `void` `Main()` ` ` `{` ` ` `int` `[] arr = { -1, -2, 4, 3, 5 };` ` ` `int` `N = arr.Length;` ` ` `Console.WriteLine(maxOfAllMins(arr, N));` ` ` `}` `}` `// This code is contributed by ukasp.` |

## Javascript

`<script>` `// Javascript code for the above approach` `// Function to find maximum of minimum value` `// of the array in the array` `// in each operation` `function` `maxOfAllMins(arr, n)` `{` ` ` `// If no operations are done` ` ` `let max_value = arr[0];` ` ` `// Sort the array arr in ascending order` ` ` `arr.sort((a, b) => a - b);` ` ` `for` `(let i = 0; i < n - 1; i++) {` ` ` `max_value = Math.max(max_value,` ` ` `arr[i + 1] - arr[i]);` ` ` `}` ` ` `return` `max_value;` `}` `// Driver code` `let arr = [ -1, -2, 4, 3, 5 ];` `let N = arr.length` `document.write(maxOfAllMins(arr, N));` `// This code is contributed by gfgking.` `</script>` |

**Output**

4

**Time Complexity:** O(NlogN)**Auxiliary Space: **O(1)