Maximize the minimum element of Array by reducing elements one by one
Given an array arr[] containing N integers. In each operation, a minimum integer is chosen from the array and deleted from the array after subtracting it from the remaining elements. The task is to find the maximum of minimum values of the array after any number of such operations.
Examples:
Input: arr[] = {-1, -2, 4, 3, 5}
Output: 4
Explanation: Following are the operations performed in array
First operation: 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(N2)
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:
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, …}
- Initially a1 is the minimum after removing it the array is {a2-a1, a3-a1, a4-a1, a5-a1, ..}
- Now a2-a1 is the minimum after removing it the array is{a3-a1-(a2-a1), a4-a1-(a2-a1), ..} which is equal to {a3-a2, a4-a2, a5-a2, …}
- Now a3-a2 is the minimum and it continues so…
res = max(a1, ∑(i=0 to N-1) (ai+1 -ai))
The end result is going to be a difference of consecutive elements as seen from the above proof. Because the minimum in each step is a difference of the adjacent elements.
Follow the steps below to solve the problem:
- Initialize the initial answer as max_value with 0 operations as arr[0].
- Sort the array arr[] in ascending order.
- Iterate in the range of [0, N-1]
- Keep track of the maximum of minimum value (i.e the difference arr[i + 1] – arr[i]) on each iteration.
- Return max_value.
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 min_after_del( 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); // Traverse the array to check // the required condition 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() { // Initializing array of arr int arr[] = { -1, -2, 4, 3, 5 }; int N = sizeof (arr) / sizeof (arr[0]); // Call the function and // print the answer cout << (min_after_del(arr, N)); return 0; } |
Java
// Java code for the above approach import java.util.*; class GFG{ // Function to find maximum of minimum value // of the array in the array // in each operation static int min_after_del( int arr[], int n) { // If no operations are done int max_value = arr[ 0 ]; // Sort the array arr in ascending order Arrays.sort(arr); // Traverse the array to check // the required condition 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) { // Initializing array of arr int arr[] = { - 1 , - 2 , 4 , 3 , 5 }; int N = arr.length; // Call the function and // print the answer System.out.print(min_after_del(arr, N)); } } // This code is contributed by 29AjayKumar |
Python3
# Python code for the above approach # Function to find maximum of minimum value # of the array in the array # in each operation def min_after_del (arr, n): # If no operations are done max_value = arr[ 0 ]; # Sort the array arr in ascending order arr.sort(); # Traverse the array to check # the required condition for i in range (n - 1 ): max_value = max (max_value, arr[i + 1 ] - arr[i]); return max_value; # Driver code # Initializing array of arr arr = [ - 1 , - 2 , 4 , 3 , 5 ]; N = len (arr) # Call the function and # print the answer print (min_after_del(arr, N)); # This code is contributed by Saurabh Jaiswal |
C#
using System; public class GFG{ // Function to find maximum of minimum value // of the array in the array // in each operation static int min_after_del( int [] arr, int n) { // If no operations are done int max_value = arr[0]; // Sort the array arr in ascending order Array.Sort(arr); // Traverse the array to check // the required condition for ( int i = 0; i < n - 1; i++) { max_value = Math.Max(max_value, arr[i + 1] - arr[i]); } return max_value; } // Driver code static public void Main (){ int [] arr = { -1, -2, 4, 3, 5 }; int N = arr.Length; // Call the function and // print the answer Console.Write(min_after_del(arr, N)); } } // This code is contributed by hrithikgarg03188. |
Javascript
<script> // JavaScript code for the above approach // Function to find maximum of minimum value // of the array in the array // in each operation const min_after_del = (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); // Traverse the array to check // the required condition for (let i = 0; i < n - 1; i++) { max_value = Math.max(max_value, arr[i + 1] - arr[i]); } return max_value; } // Driver code // Initializing array of arr let arr = [-1, -2, 4, 3, 5]; let N = arr.length; // Call the function and // print the answer document.write(min_after_del(arr, N)); // This code is contributed by rakeshsahni </script> |
4
Time Complexity : O(N log(N))
Space Complexity : O(1)
Please Login to comment...