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++
// CPP program to find the maximum possible // value of the minimum value of // modified array #include <bits/stdc++.h> 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
<?php // PHP 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 function maxOfMin($a, $n, $S) { // To store minimum value // of array $mi = PHP_INT_MAX; // To store sum of elements // of array $s1 = 0; for ($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 $low = 0; // maximum possible value $high = $mi; // to store a required answer $ans; // 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 = array( 10, 10, 10, 10, 10 ); $S = 10; $n = sizeof($a); echo maxOfMin($a, $n, $S); // This code is contributed by akt_mit ?> |
8
Recommended Posts:
- Maximum modified Array sum possible by choosing elements as per the given conditions
- Minimize the difference between the maximum and minimum values of the modified array
- Minimum possible value T such that at most D Partitions of the Array having at most sum T is possible
- Maximum XOR value of maximum and second maximum element among all possible subarrays
- Find the modified array after performing k operations of given type
- Kth smallest element in the array using constant space when array can't be modified
- Find the Nth element of the modified Fibonacci series
- Check if a king can move a valid move or not when N nights are there in a modified chessboard
- A modified game of Nim
- Sum of nth terms of Modified Fibonacci series made by every pair of two arrays
- Predictor-Corrector or Modified-Euler method for solving Differential equation
- Sum of all the numbers present at given level in Modified Pascal’s triangle
- Program to print modified Binary triangle pattern
- Minimum LCM and GCD possible among all possible sub-arrays
- Find the value of N when F(N) = f(a)+f(b) where a+b is the minimum possible and a*b = N
- Find the maximum possible value of a[i] % a[j] over all pairs of i and j
- Find the maximum possible value for the given periodic function
- Find minimum value to assign all array elements so that array product becomes greater
- Minimum and maximum possible length of the third side of a triangle
- Minimum distance between the maximum and minimum element of a given Array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


