Maximize minimum array element possible by exactly K decrements
Last Updated :
20 Jul, 2021
Given an array arr[] consisting of N integers and an integer K, the task is to maximize the minimum element of the array after decrementing array elements exactly K number of times.
Examples:
Input: arr[] = {2, 4, 4}, K = 3
Output: 2
Explanation:
One of the possible way is:
- Decrement arr[2] by 1. The array modifies to {2, 4, 3}.
- Decrement arr[1] by 1. The array modifies to {2, 3, 3}.
- Decrement arr[2] by 1. The array modifies to {2, 3, 2}.
Therefore, the minimum array element that can be obtained is 2, which it is the maximum possible value.
Input: arr[] = {10, 10, 10, 10, 10}, K = 10
Output: 8
Naive Approach: The simplest approach to solve the given problem is to iterate over the range [1, K] and in each iteration, find the maximum element of the array and then decrement it by 1. After the above steps then print the minimum element of the array.
Time Complexity: O(N * K)
Auxiliary Space: O(1)
Efficient Approach: The above problem can also be optimized based on the following observations:
- It can be observed that the optimal way is to first decrement all the values of the array to the minimum element of the array.
- The total moves needed to make all the elements equal to the min-element is the sum of the array decremented by K times the minimum element of the array.
- If the total number of moves needed to make all the elements equal to the minimum element is less than K then the minimum element will be the answer.
- Otherwise, it will be optimal to decrement from each element of the array by 1 until K becomes 0. Then minimum element will be equal to .
Follow the steps to solve the problem:
- Find the minimum element of the array arr[] and store it in a variable say minElement.
- Initialize a variable, say reqOperation as 0 to store the total number of moves needed to make all the elements equal to the minElement of the array.
- Traverse the array arr[] and in each iteration, increment reqOperation by the current element of the array subtracted by the minElement.
- If reqOperation is greater than K then print minElement. Otherwise, print the value of minElement – (K + N – 1) / N as the resultant minimum element.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minimumElement( int arr[], int N,
int K)
{
int minElement = arr[0];
for ( int i = 0; i < N; ++i) {
minElement = min(minElement,
arr[i]);
}
int reqOperations = 0;
for ( int i = 0; i < N; ++i) {
reqOperations += arr[i] - minElement;
}
if (reqOperations < K) {
K -= reqOperations;
minElement -= (K + N - 1) / N;
}
return minElement;
}
int main()
{
int arr[] = { 10, 10, 10, 10 };
int K = 7;
int N = sizeof (arr) / sizeof (arr[0]);
cout << minimumElement(arr, N, K);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int minimumElement( int arr[], int N, int K)
{
int minElement = arr[ 0 ];
for ( int i = 0 ; i < N; ++i) {
minElement = Math.min(minElement, arr[i]);
}
int reqOperations = 0 ;
for ( int i = 0 ; i < N; ++i) {
reqOperations += arr[i] - minElement;
}
if (reqOperations < K) {
K -= reqOperations;
minElement -= (K + N - 1 ) / N;
}
return minElement;
}
public static void main(String[] args)
{
int arr[] = { 10 , 10 , 10 , 10 };
int K = 7 ;
int N = arr.length;
System.out.println(minimumElement(arr, N, K));
}
}
|
Python3
def minimumElement(arr, N, K):
minElement = arr[ 0 ];
for i in range (N):
minElement = min (minElement, arr[i]);
reqOperations = 0 ;
for i in range (N):
reqOperations + = arr[i] - minElement
if (reqOperations < K):
K - = reqOperations;
minElement - = (K + N - 1 ) / / N;
return minElement;
arr = [ 10 , 10 , 10 , 10 ];
K = 7 ;
N = len (arr)
print (minimumElement(arr, N, K));
|
C#
using System;
class GFG
{
static int minimumElement( int []arr, int N, int K)
{
int minElement = arr[0];
for ( int i = 0; i < N; ++i) {
minElement = Math.Min(minElement, arr[i]);
}
int reqOperations = 0;
for ( int i = 0; i < N; ++i) {
reqOperations += arr[i] - minElement;
}
if (reqOperations < K) {
K -= reqOperations;
minElement -= (K + N - 1) / N;
}
return minElement;
}
public static void Main( string [] args)
{
int [] arr= { 10, 10, 10, 10 };
int K = 7;
int N = arr.Length;
Console.Write(minimumElement(arr, N, K));
}
}
|
Javascript
<script>
function minimumElement(arr, N, K)
{
let minElement = arr[0];
for (let i = 0; i < N; ++i)
{
minElement = Math.min(minElement,
arr[i]);
}
let reqOperations = 0;
for (let i = 0; i < N; ++i)
{
reqOperations += arr[i] - minElement;
}
if (reqOperations < K)
{
K -= reqOperations;
minElement -= Math.floor((K + N - 1) / N);
}
return minElement;
}
let arr = [ 10, 10, 10, 10 ];
let K = 7;
let N = arr.length;
document.write(minimumElement(arr, N, K));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...