给定大小数组还有一个数字 。任务是修改给定的数组,以便:
- 修改前后的数组元素之和之差恰好等于S。
- 修改后的数组元素应为非负数。
- 修改后的数组中的最小值必须最大化。
- 要修改给定的数组,可以增加或减少该数组的任何元素。
任务是找到修改后的数组的最小数量。如果不可能,则打印-1。最小数量应尽可能大。
例子:
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
一种有效的方法是在修改后的数组中的最小数的最小值和最大值之间进行二进制搜索。最小可能值为零,最大可能数组为给定数组中的最小数量。如果给定的数组元素的总和小于S,则不可能回答。因此,打印-1。如果给定的数组元素之和等于S,则答案将为零。
下面是上述方法的实现:
C++
// CPP program to find the maximum possible
// value of the minimum value of
// modified array
#include
using namespace std;
// Function to find the maximum possible value
// of the minimum value of the modified array
int maxOfMin(int a[], int n, int S)
{
// To store minimum value of array
int mi = INT_MAX;
// To store sum of elements of array
int s1 = 0;
for (int i = 0; i < n; i++) {
s1 += a[i];
mi = min(a[i], mi);
}
// Solution is not possible
if (s1 < S)
return -1;
// zero is the possible value
if (s1 == S)
return 0;
// minimum possible value
int low = 0;
// maximum possible value
int high = mi;
// to store a required answer
int ans;
// Binary Search
while (low <= high) {
int mid = (low + high) / 2;
// If mid is possible then try to increase
// required answer
if (s1 - (mid * n) >= S) {
ans = mid;
low = mid + 1;
}
// If mid is not possible then decrease
// required answer
else
high = mid - 1;
}
// Return required answer
return ans;
}
// Driver Code
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
// Java program to find the maximum possible
// value of the minimum value of
// modified array
import java.io.*;
class GFG {
// Function to find the maximum possible value
// of the minimum value of the modified array
static int maxOfMin(int a[], int n, int S)
{
// To store minimum value of array
int mi = Integer.MAX_VALUE;
// To store sum of elements of array
int s1 = 0;
for (int i = 0; i < n; i++) {
s1 += a[i];
mi = Math.min(a[i], mi);
}
// Solution is not possible
if (s1 < S)
return -1;
// zero is the possible value
if (s1 == S)
return 0;
// minimum possible value
int low = 0;
// maximum possible value
int high = mi;
// to store a required answer
int ans=0;
// Binary Search
while (low <= high) {
int mid = (low + high) / 2;
// If mid is possible then try to increase
// required answer
if (s1 - (mid * n) >= S) {
ans = mid;
low = mid + 1;
}
// If mid is not possible then decrease
// required answer
else
high = mid - 1;
}
// Return required answer
return ans;
}
// Driver Code
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));
}
//This code is contributed by ajit.
}
Python
# Python program to find the maximum possible
# value of the minimum value of
# modified array
# Function to find the maximum possible value
# of the minimum value of the modified array
def maxOfMin(a, n, S):
# To store minimum value of array
mi = 10**9
# To store sum of elements of array
s1 = 0
for i in range(n):
s1 += a[i]
mi = min(a[i], mi)
# Solution is not possible
if (s1 < S):
return -1
# zero is the possible value
if (s1 == S):
return 0
# minimum possible value
low = 0
# maximum possible value
high = mi
# to store a required answer
ans=0
# Binary Search
while (low <= high):
mid = (low + high) // 2
# If mid is possible then try to increase
# required answer
if (s1 - (mid * n) >= S):
ans = mid
low = mid + 1
# If mid is not possible then decrease
# required answer
else:
high = mid - 1
# Return required answer
return ans
# Driver Code
a=[10, 10, 10, 10, 10]
S = 10
n =len(a)
print(maxOfMin(a, n, S))
#This code is contributed by Mohit kumar 29
C#
// C# program to find the maximum possible
// value of the minimum value of
// modified array
using System;
class GFG {
// Function to find the maximum possible value
// of the minimum value of the modified array
static int maxOfMin(int []a, int n, int S)
{
// To store minimum value of array
int mi = int.MaxValue;
// To store sum of elements of array
int s1 = 0;
for (int i = 0; i < n; i++) {
s1 += a[i];
mi = Math.Min(a[i], mi);
}
// Solution is not possible
if (s1 < S)
return -1;
// zero is the possible value
if (s1 == S)
return 0;
// minimum possible value
int low = 0;
// maximum possible value
int high = mi;
// to store a required answer
int ans=0;
// Binary Search
while (low <= high) {
int mid = (low + high) / 2;
// If mid is possible then try to increase
// required answer
if (s1 - (mid * n) >= S) {
ans = mid;
low = mid + 1;
}
// If mid is not possible then decrease
// required answer
else
high = mid - 1;
}
// Return required answer
return ans;
}
// Driver Code
public static void Main () {
int []a = { 10, 10, 10, 10, 10 };
int S = 10;
int n = a.Length;
Console.WriteLine(maxOfMin(a, n, S));
}
//This code is contributed by Ryuga
}
PHP
= $S)
{
$ans = $mid;
$low = $mid + 1;
}
// If mid is not possible then
// decrease required answer
else
$high = $mid - 1;
}
// Return required answer
return $ans;
}
// Driver Code
$a = array( 10, 10, 10, 10, 10 );
$S = 10;
$n = sizeof($a);
echo maxOfMin($a, $n, $S);
// This code is contributed by akt_mit
?>
输出:
8