#include <bits/stdc++.h>
using namespace std;
// Function to check if maxx can be a valid value
bool isValid(int maxx, vector<int>& arr, int k)
{
// Initialize a variable sum = 0
long long operationCount = 0;
// Iterate over the array arr[]
for (long long i = 0; i < arr.size(); i++) {
// Keep storing the maximum value in sum
if (arr[i] > maxx)
operationCount += arr[i] - maxx;
// Check if number of operation required to
// make maximum element at max maxx.
// If true, return false
if (operationCount > k)
return false;
}
// Return true
return true;
}
// Function to find the minimum possible maximum
int minimizeArrayMaxValue(vector<int>& arr, int k)
{
long long n = arr.size();
// Initialise a variable start = 0,
// end = max value in, arr[] and result
long long start = *min_element(arr.begin(), arr.end()),
end = *max_element(arr.begin(), arr.end());
long long result;
// Do while start is less than equals to end
while (start <= end) {
// Calculate the mid by (start + end) / 2;
long long mid = (start + end) / 2;
// Check if mid can be a valid max Value.
if (isValid(mid, arr, k)) {
// Shift end to mid - 1
end = mid - 1;
// Store mid into result
result = mid;
cout<<result;
}
// Otherwise, shift start to mid + 1
else {
start = mid + 1;
}
}
// Return the result
return result;
}
// Driver code
int main()
{
vector<int> arr = { 3, 3, 7, 1, 6 };
int K = 3;
// Function call
cout << minimizeArrayMaxValue(arr, K);
return 0;
}
// java implementation
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
class GFG
{
// Function to check if maxx can be a valid value
public static boolean
isValid(int maxx, ArrayList<Integer> arr, int k)
{
// Initialize a variable sum = 0
int operationCount = 0;
// Iterate over the array arr[]
for (int i = 0; i < arr.size(); i++) {
// Keep storing the maximum value in sum
if (arr.get(i) > maxx)
operationCount += arr.get(i) - maxx;
// Check if number of operation required to
// make maximum element at max maxx.
// If true, return false
if (operationCount > k)
return false;
}
// Return true
return true;
}
// Function to find the minimum possible maximum
public static int
minimizeArrayMaxValue(ArrayList<Integer> arr, int k)
{
int n = arr.size();
// Initialise a variable start = 0,
// end = max value in, arr[] and result
int start = Collections.min(arr);
int end = Collections.max(arr);
int result = 0;
// Do while start is less than equals to end
while (start <= end) {
// Calculate the mid by (start + end) / 2;
int mid = (int)((start + end) / 2);
// Check if mid can be a valid max Value.
if (isValid(mid, arr, k)) {
// Shift end to mid - 1
end = mid - 1;
// Store mid into result
result = mid;
}
// Otherwise, shift start to mid + 1
else {
start = mid + 1;
}
}
// Return the result
return result;
}
public static void main(String[] args)
{
int n = 5;
ArrayList<Integer> arr = new ArrayList<Integer>(n);
arr.add(3);
arr.add(3);
arr.add(7);
arr.add(1);
arr.add(6);
int K = 3;
// Function call
System.out.println(minimizeArrayMaxValue(arr, K));
}
}
// This code is contributed by ksam24000
# Function to check if maxx can be a valid value
def isValid(maxx, arr, k):
# Initialize a variable sum = 0
operationCount = 0
# Iterate over the array arr[]
for i in range(len(arr)):
# Keep storing the maximum value in sum
if arr[i] > maxx:
operationCount += arr[i] - maxx
# Check if number of operation required to
# make maximum element at max maxx.
# If true, return false
if operationCount > k:
return False
# Return true
return True
# Function to find the minimum possible maximum
def minimizeArrayMaxValue(arr, k):
# Initialise a variable start = 0,
# end = max value in, arr[] and result
start = min(arr)
end = max(arr)
result = 0
# Do while start is less than equals to end
while start <= end:
# Calculate the mid by (start + end) / 2
mid = (start + end) // 2
# Check if mid can be a valid max Value.
if isValid(mid, arr, k):
# Shift end to mid - 1
end = mid - 1
# Store mid into result
result = mid
# Otherwise, shift start to mid + 1
else:
start = mid + 1
# Return the result
return result
# Driver code
arr = [3, 3, 7, 1, 6]
K = 3
# Function call
print(minimizeArrayMaxValue(arr, K))
# This code is contributed by Tapesh(tapeshdua420)
// Include namespace system
using System;
using System.Linq;
using System.Collections.Generic;
public class GFG
{
// Function to check if maxx can be a valid value
public static bool isValid(int maxx, int[] arr, int k)
{
// Initialize a variable sum = 0
var operationCount = 0;
// Iterate over the array arr[]
for (int i = 0; i < arr.Length; i++)
{
// Keep storing the maximum value in sum
if (arr[i] > maxx)
{
operationCount += arr[i] - maxx;
}
// Check if number of operation required to
// make maximum element at max maxx.
// If true, return false
if (operationCount > k)
{
return false;
}
}
// Return true
return true;
}
// Function to find the minimum possible maximum
public static int minimizeArrayMaxValue(int[] arr, int k)
{
var n = arr.Length;
// Initialise a variable start = 0,
// end = max value in, arr[] and result
var start = arr.Min();
var end = arr.Max();
var result = 0;
// Do while start is less than equals to end
while (start <= end)
{
// Calculate the mid by (start + end) / 2;
var mid = (int)((int)((start + end) / 2));
// Check if mid can be a valid max Value.
if (GFG.isValid(mid, arr, k))
{
// Shift end to mid - 1
end = mid - 1;
// Store mid into result
result = mid;
}
else
{
start = mid + 1;
}
}
// Return the result
return result;
}
public static void Main(String[] args)
{
var arr = new int[]{3,3,7,1,6};
var K = 3;
// Function call
Console.WriteLine(GFG.minimizeArrayMaxValue(arr, K));
}
}
// This code is contributed by aadityaburujwale.
// Include namespace system
// JS implementation
// Function to check if maxx can be a valid value
function isValid(maxx, arr, k)
{
// Initialize a variable sum = 0
let operationCount = 0;
// Iterate over the array arr[]
for (let i = 0; i < arr.length; i++)
{
// Keep storing the maximum value in sum
if (arr[i] > maxx)
{
operationCount += arr[i] - maxx;
}
// Check if number of operation required to
// make maximum element at max maxx.
// If true, return false
if (operationCount > k)
{
return false;
}
}
// Return true
return true;
}
// Function to find the minimum possible maximum
function minimizeArrayMaxValue(arr,k)
{
let n = arr.length;
// Initialise a variable start = 0,
// end = max value in, arr[] and result
let start = Math.min.apply(Math, arr);
let end = Math.max.apply(Math, arr);
let result = 0;
// Do while start is less than equals to end
while (start <= end)
{
// Calculate the mid by (start + end) / 2;
let mid = Math.floor((start + end) / 2);
// Check if mid can be a valid max Value.
if (isValid(mid, arr, k))
{
// Shift end to mid - 1
end = mid - 1;
// Store mid into result
result = mid;
}
else
{
start = mid + 1;
}
}
// Return the result
return result;
}
let arr = [3,3,7,1,6];
let K = 3;
// Function call
console.log(minimizeArrayMaxValue(arr, K));
// This code is contributed by ksam24000