Find the maximum possible value of the minimum value of modified array
Given an array of size and a number . The task is to modify the given array such that:
- The difference between the sum of the array elements before and after modification is exactly equal to S.
- Modified array elements should be non-negative.
- The minimum value in the modified array must be maximized.
- To modify the given array, you can increment or decrement any element of the array.
The task is to find the minimum number of the modified array. If it is not possible then print -1. The minimum number should be as maximum as possible.
Examples:
Input : a[] = {2, 2, 3}, S = 1
Output : 2
Explanation : Modified array is {2, 2, 2}
Input : a[] = {1, 3, 5}, S = 10
Output : -1
An efficient approach is to make a binary search between the minimum and the maximum possible value of the minimum number in a modified array. The minimum possible value is zero and the maximum possible array is minimum number in a given array. If given array elements sum is less than S then answer is not possible. so, print -1. If given array elements sum equals to S then answer will be zero.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxOfMin( int a[], int n, int S)
{
int mi = INT_MAX;
int s1 = 0;
for ( int i = 0; i < n; i++) {
s1 += a[i];
mi = min(a[i], mi);
}
if (s1 < S)
return -1;
if (s1 == S)
return 0;
int low = 0;
int high = mi;
int ans;
while (low <= high) {
int mid = (low + high) / 2;
if (s1 - (mid * n) >= S) {
ans = mid;
low = mid + 1;
}
else
high = mid - 1;
}
return ans;
}
int main()
{
int a[] = { 10, 10, 10, 10, 10 };
int S = 10;
int n = sizeof (a) / sizeof (a[0]);
cout << maxOfMin(a, n, S);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int maxOfMin( int a[], int n, int S)
{
int mi = Integer.MAX_VALUE;
int s1 = 0 ;
for ( int i = 0 ; i < n; i++) {
s1 += a[i];
mi = Math.min(a[i], mi);
}
if (s1 < S)
return - 1 ;
if (s1 == S)
return 0 ;
int low = 0 ;
int high = mi;
int ans= 0 ;
while (low <= high) {
int mid = (low + high) / 2 ;
if (s1 - (mid * n) >= S) {
ans = mid;
low = mid + 1 ;
}
else
high = mid - 1 ;
}
return ans;
}
public static void main (String[] args) {
int a[] = { 10 , 10 , 10 , 10 , 10 };
int S = 10 ;
int n = a.length;
System.out.println( maxOfMin(a, n, S));
}
}
|
Python
def maxOfMin(a, n, S):
mi = 10 * * 9
s1 = 0
for i in range (n):
s1 + = a[i]
mi = min (a[i], mi)
if (s1 < S):
return - 1
if (s1 = = S):
return 0
low = 0
high = mi
ans = 0
while (low < = high):
mid = (low + high) / / 2
if (s1 - (mid * n) > = S):
ans = mid
low = mid + 1
else :
high = mid - 1
return ans
a = [ 10 , 10 , 10 , 10 , 10 ]
S = 10
n = len (a)
print (maxOfMin(a, n, S))
|
C#
using System;
class GFG {
static int maxOfMin( int []a, int n, int S)
{
int mi = int .MaxValue;
int s1 = 0;
for ( int i = 0; i < n; i++) {
s1 += a[i];
mi = Math.Min(a[i], mi);
}
if (s1 < S)
return -1;
if (s1 == S)
return 0;
int low = 0;
int high = mi;
int ans=0;
while (low <= high) {
int mid = (low + high) / 2;
if (s1 - (mid * n) >= S) {
ans = mid;
low = mid + 1;
}
else
high = mid - 1;
}
return ans;
}
public static void Main () {
int []a = { 10, 10, 10, 10, 10 };
int S = 10;
int n = a.Length;
Console.WriteLine(maxOfMin(a, n, S));
}
}
|
PHP
<?php
function maxOfMin( $a , $n , $S )
{
$mi = PHP_INT_MAX;
$s1 = 0;
for ( $i = 0; $i < $n ; $i ++)
{
$s1 += $a [ $i ];
$mi = min( $a [ $i ], $mi );
}
if ( $s1 < $S )
return -1;
if ( $s1 == $S )
return 0;
$low = 0;
$high = $mi ;
$ans ;
while ( $low <= $high )
{
$mid = ( $low + $high ) / 2;
if ( $s1 - ( $mid * $n ) >= $S )
{
$ans = $mid ;
$low = $mid + 1;
}
else
$high = $mid - 1;
}
return $ans ;
}
$a = array ( 10, 10, 10, 10, 10 );
$S = 10;
$n = sizeof( $a );
echo maxOfMin( $a , $n , $S );
?>
|
Javascript
<script>
function maxOfMin(a, n, S)
{
let mi = Number.MAX_VALUE;
let s1 = 0;
for (let i = 0; i < n; i++) {
s1 += a[i];
mi = Math.min(a[i], mi);
}
if (s1 < S)
return -1;
if (s1 == S)
return 0;
let low = 0;
let high = mi;
let ans=0;
while (low <= high) {
let mid = parseInt((low + high) / 2, 10);
if (s1 - (mid * n) >= S) {
ans = mid;
low = mid + 1;
}
else
high = mid - 1;
}
return ans;
}
let a = [ 10, 10, 10, 10, 10 ];
let S = 10;
let n = a.length;
document.write(maxOfMin(a, n, S));
</script>
|
Time Complexity: O(n + logn)
Auxiliary Space: O(1)
Last Updated :
25 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...