Java Program to Minimize the Maximum Element of an Array
Last Updated :
29 May, 2021
Given two integers N and K. Create an array of N positive integers such that the sum of all elements of the array is divisible by K and the maximum element in the array is the minimum possible.
You have to find the Maximum element of that array.
Example:
Input : N=5, K=11
Output : 3
Explanation : We can create the array as [2, 2, 2, 2, 3]. The sum of the array is 11 which is divisible by K=11 and the maximum element is 3 which is minimum possible in this case.
Input : N=4, K=3
Output : 2
Explanation : We can create the array as [1, 2, 1, 2]. The sum of the array is 6 which is divisible by K=3 and the maximum element is 2 which is minimum possible in this case.
Approach :
Since we have to take all the N numbers positive, so the minimum value we can take is 1.So, initially, the sum of the array is N*1 = N.
Now, there exist two possible cases -
- If K is greater than or equal N: If we make the sum of the array is equal to K, then we can observe the maximum element will also be minimized. So, now we have to form an array consisting of N positive integers whose Sum is K. We can distribute K/N to all the N places. If the sum of the array is still not equal to K then, we will increment one element by K-(N*(K/N)). Thus, we can find the minimum possible maximum element. And, we are distributing K/N values to each place so that any element does not become so big.
- If K is less than N: Since the initial sum is N, so we have increment our Sum such that Sum is divisible by K and the Sum is the minimum possible. The Sum has to be minimum because we have to minimize our maximum element too. Let's say the optimal Sum is S. So, S has to divisible by K and S will be greater than or equal to N. Now, this has become the same as the 1st case.
Let, in both cases, the Optimal sum is S. So, giving the floor value of S/N to N-1 elements and giving the ceil value of sum/K to the rest element will make the sum equal to S and it is also divisible by K.
Now, Between the floor value of sum/N and the ceiling value of sum/N, ceil value will be greater. Finally, the maximum element will the ceil value of sum/N.
Below is the implementation of the above approach :
Java
// Java Program to Minimize the maximum element of an array
import java.io.*;
import java.lang.*;
import java.util.*;
public class Main {
public static void main(String[] args)
{
// Given
int N = 5;
int K = 11;
System.out.println("N is " +N);
System.out.println("K is " +K);
// variable sum stores the optimal Sum
int sum = 0;
// the first case
if (K >= N) {
// the optimal sum will be K
// as we have to keep the sum as minimum as
// possible and the sum has to divisible by K
sum = K;
}
// the second case
else {
// we have to increment sum as
// the sum is divisible by K
// and sum is greater than or equal to N
// the ceiling value of N/K will give the
// minimum value that has to be multiplied by K
// to get the optimal sum
int times = (int)Math.ceil((double)N / (double)K);
sum = times * K;
}
// maximum_element will the ceil value of sum/N
int maximum_element
= (int)Math.ceil((double)sum / (double)N);
// print the maximum_element as answer
System.out.println("Maximum element is " +maximum_element);
}
}
OutputN is 5
K is 11
Maximum element is 3
Time Complexity: O(1)
Auxiliary Space: O(1)
Similar Reads
Java Program to Print the Smallest Element in an Array Java provides a data structure, the array, which stores the collection of data of the same type. It is a fixed-size sequential collection of elements of the same type. Example: arr1[] = {2 , -1 , 9 , 10} output : -1 arr2[] = {0, -10, -13, 5} output : -13 We need to find and print the smallest value
3 min read
Java Program for Maximize elements using another array Given two arrays with size n, maximize the first array by using the elements from the second array such that the new array formed contains n greatest but unique elements of both the arrays giving the second array priority (All elements of second array appear before first array). The order of appeara
3 min read
Java Program to Get the Maximum Element From a Vector Prerequisite: Vectors in Java Why We Use Vector? Till now, we have learned two ways for declaring either with a fixed size of array or size enter as per the demand of the user according to which array is allocated in memory. int Array_name[Fixed_size] ; int array_name[variable_size] ; Both ways we l
4 min read
Java Program to Find Largest Element in an Array Finding the largest element in an array is a common programming task. There are multiple approaches to solve it. In this article, we will explore four practical approaches one by one to solve this in Java.Example Input/Output:Input: arr = { 1, 2, 3, 4, 5}Output: 5Input: arr = { 10, 3, 5, 7, 2, 12}Ou
4 min read
Java Program to Find maximum element of each row in a matrix Given a matrix, the task is to find the maximum element of each row. Examples: Input : [1, 2, 3] [1, 4, 9] [76, 34, 21] Output : 3 9 76 Input : [1, 2, 3, 21] [12, 1, 65, 9] [1, 56, 34, 2] Output : 21 65 56 Approach : Approach is very simple. The idea is to run the loop for no_of_rows. Check each ele
2 min read