Maximise minimum element possible in Array after performing given operations
Last Updated :
12 Dec, 2022
Given an array arr[] of size N. The task is to maximize the minimum value of the array after performing given operations. In an operation, value x can be chosen and
- A value 3 * x can be subtracted from the arr[i] element.
- A value x is added to arr[i-1]. and
- A value of 2 * x can be added to arr[i-2].
Find the maximum possible minimum element of the array after any such operations.
Examples:
Input: arr[] = {1, 2, 3, 4, 5, 6}
Output: 3
Explanation: The last element is chosen and x =1 can be chosen.
So 3*x gets subtracted from arr[i] and x gets added to arr[i-1] and 2*x to arr[i-2] so the array becomes {1, 2, 3, 6, 6, 3}
In the 4th index x =1 can be chosen and now the array becomes {1, 2, 5, 7, 3, 3}.
In the 3rd index x = 1 can be chosen and now the array becomes {1, 4, 6, 4, 3, 3}.
In the 2nd index again x =1 can be chosen and now the array becomes {3, 4, 3, 4, 3, 3, 3}.
Hence the maximum possible minimum value is 3.
Input: arr[] = {9, 13, 167}
Output: 51
Naive Approach: This problem can be solved by checking for the possibility of maximum possible minimum value from [1, max(array)] by performing the operations from the end of the array.
Time Complexity: O(N2)
Space Complexity: O(1)
Efficient Approach: The efficient approach of this problem is based on Binary Search. Since it is based on maximizing the minimum value so by applying the binary search in the range [1, max(array)] and checking if mid is possible as a minimum element by performing the operations on the array such that every element is >=mid. Follow the steps below to solve the given problem:
- Initialize f = 1 and l = maximum element of the array and the res as INT_MIN.
- Perform binary search while f<=l
- Check if mid can be the minimum element by performing operations in the is_possible_min() function.
- In the is_possible_min() function
- Traverse from the end of the array (N-1) till index 2 and check if arr[i]<mid if it true return 0.
- Else find the extra which is 3x that can be added to arr[i-1] as x and arr[i-2] as 2x.
- If arr[0] >=mid and arr[1] >=mid return 1.
- Else return 0.
- If the is_possible_min() function returns true then mid is possible as the minimum value store the max(res, mid) in the res variable, so maximize the minimum value by moving right as f=mid +1
- Else move towards left and try if it is possible by l = mid -1.
- Print the res.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
bool is_possible_min(vector< int > arr,
int mid)
{
int N = arr.size();
for ( int i = N - 1; i >= 2; i--) {
if (arr[i] < mid)
return 0;
else {
int extra = arr[i] - mid;
extra /= 3;
arr[i - 1] += extra;
arr[i - 2] += 2 * extra;
}
}
if (arr[0] >= mid && arr[1] >= mid)
return 1;
return 0;
}
void find_maximum_min(vector< int > arr)
{
int f = 1, l = *max_element(arr.begin(),
arr.end());
int res = INT_MIN;
while (f <= l) {
int mid = (f + l) / 2;
if (is_possible_min(arr, mid)) {
res = max(res, mid);
f = mid + 1;
}
else {
l = mid - 1;
}
}
cout << res << endl;
}
int main()
{
vector< int > arr = { 1, 2, 3, 4, 5, 6 };
find_maximum_min(arr);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
static Boolean is_possible_min( int arr[],
int mid)
{
int N = arr.length;
for ( int i = N - 1 ; i >= 2 ; i--) {
if (arr[i] < mid)
return false ;
else {
int extra = arr[i] - mid;
extra /= 3 ;
arr[i - 1 ] += extra;
arr[i - 2 ] += 2 * extra;
}
}
if (arr[ 0 ] >= mid && arr[ 1 ] >= mid)
return true ;
return false ;
}
static void find_maximum_min( int arr[])
{
int f = 1 , l = Arrays.stream(arr).max().getAsInt();
int res = Integer.MIN_VALUE;
while (f <= l) {
int mid = l + (f - l) / 2 ;
if (is_possible_min(arr, mid) == true ) {
res = Math.max(res, mid);
f = mid + 1 ;
}
else {
l = mid - 1 ;
}
}
System.out.println(res);
}
public static void main (String[] args)
{
int arr[] = { 1 , 2 , 3 , 4 , 5 , 6 };
find_maximum_min(arr);
}
}
|
Python3
INT_MIN = - 2147483647 - 1
def is_possible_min(arr, mid):
N = len (arr)
for i in range (N - 1 , 1 , - 1 ):
if (arr[i] < mid):
return 0
else :
extra = arr[i] - mid
extra / / = 3
arr[i - 1 ] + = extra
arr[i - 2 ] + = 2 * extra
if (arr[ 0 ] > = mid and arr[ 1 ] > = mid):
return 1
return 0
def find_maximum_min(arr):
f, l = 1 , max (arr)
res = INT_MIN
while (f < = l):
mid = (f + l) / / 2
if (is_possible_min(arr.copy(), mid)):
res = max (res, mid)
f = mid + 1
else :
l = mid - 1
print (res)
if __name__ = = "__main__" :
arr = [ 1 , 2 , 3 , 4 , 5 , 6 ]
find_maximum_min(arr)
|
C#
using System;
using System.Linq;
class GFG
{
static bool is_possible_min( int [] arr, int mid)
{
int N = arr.Length;
for ( int i = N - 1; i >= 2; i--) {
if (arr[i] < mid)
return false ;
else {
int extra = arr[i] - mid;
extra /= 3;
arr[i - 1] += extra;
arr[i - 2] += 2 * extra;
}
}
if (arr[0] >= mid && arr[1] >= mid)
return true ;
return false ;
}
static void find_maximum_min( int [] arr)
{
int f = 1, l = arr.Max();
int res = Int32.MinValue;
while (f <= l) {
int mid = l + (f - l) / 2;
if (is_possible_min(arr, mid) == true ) {
res = Math.Max(res, mid);
f = mid + 1;
}
else {
l = mid - 1;
}
}
Console.WriteLine(res);
}
public static void Main()
{
int [] arr = { 1, 2, 3, 4, 5, 6 };
find_maximum_min(arr);
}
}
|
Javascript
<script>
function is_possible_min(arr, mid) {
let N = arr.length;
for (let i = N - 1; i >= 2; i--) {
if (arr[i] < mid)
return 0;
else {
let extra = arr[i] - mid;
extra = Math.floor(extra / 3);
arr[i - 1] += extra;
arr[i - 2] += 2 * extra;
}
}
if (arr[0] >= mid && arr[1] >= mid)
return 1;
return 0;
}
function find_maximum_min(arr) {
let f = 1, l = max_element(arr);
let res = Number.MIN_SAFE_INTEGER
while (f <= l) {
let mid = Math.ceil((f + l) / 2);
if (is_possible_min(arr, mid)) {
res = Math.max(res, mid);
f = mid + 1;
}
else {
l = mid - 1;
}
}
document.write(res);
}
function max_element(ar) {
return [...ar].sort((a, b) => - a + b)[0]
}
let arr = [1, 2, 3, 4, 5, 6];
find_maximum_min(arr);
</script>
|
Time Complexity: O(N* log(maxval)) where maxval is the maximum element of the array. As we are using a while loop to traverse log(maxval) times as we are decrementing by floor division of 2 every traversal, and is_possible_min function will cost O (N) as we are using a loop to traverse N times.
Auxiliary Space: O(1), as we are not using any extra space.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...