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 Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s
10 min read
Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per
15+ min read
Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it,
13 min read
Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me
15+ min read
Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an
13 min read
Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac
15+ min read
Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt
10 min read
Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its
8 min read
Java Interface An Interface in Java programming language is defined as an abstract type used to specify the behaviour of a class. An interface in Java is a blueprint of a behaviour. A Java interface contains static constants and abstract methods. Key Properties of Interface:The interface in Java is a mechanism to
12 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read