Java Program to Minimize the Maximum Element of an Array Last Updated : 29 May, 2021 Comments Improve Suggest changes Like Article Like Report 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) Comment More infoAdvertise with us Next Article Java Program to Minimize the Maximum Element of an Array jyoti369 Follow Improve Article Tags : Java Technical Scripter Java Programs Technical Scripter 2020 Java-Array-Programs +1 More Practice Tags : Java 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 Java Program to Find k maximum elements of array in original order Given an array arr[] and an integer k, we need to print k maximum elements of given array. The elements should printed in the order of the input.Note : k is always less than or equal to n. Examples: Input : arr[] = {10 50 30 60 15} k = 2 Output : 50 60 The top 2 elements are printed as per their app 4 min read How to Find the Maximum Element in an Array? In Java, the array is a data structure that allows the users to store data of the same type in contiguous memory locations. To find the maximum element in an Array in Java, we can sort the array in ascending order using the Arrays.sort() method and then we can access the last element of the array wh 1 min read Java Program for Maximum equilibrium sum in an array Given an array arr[]. Find the maximum value of prefix sum which is also suffix sum for index i in arr[]. Examples : Input : arr[] = {-1, 2, 3, 0, 3, 2, -1} Output : 4 Prefix sum of arr[0..3] = Suffix sum of arr[3..6] Input : arr[] = {-2, 5, 3, 1, 2, 6, -4, 2} Output : 7 Prefix sum of arr[0..3] = Su 4 min read Java Program for Maximum and Minimum in a square matrix. Given a square matrix of order n*n, find the maximum and minimum from the matrix given. Examples: Input : arr[][] = {5, 4, 9, 2, 0, 6, 3, 1, 8}; Output : Maximum = 9, Minimum = 0 Input : arr[][] = {-5, 3, 2, 4}; Output : Maximum = 4, Minimum = -5 Naive Method : We find maximum and minimum of matrix 3 min read Finding the Minimum or Maximum Value in Java ArrayList The minimum value is the one with the smallest value and the maximum value is the one with the largest value. The main task here is to find the minimum and maximum value from the ArrayList. Consider an example of an ArrayList, and we need to find the largest and the smallest element. Example: Input 5 min read Like