Minimize the maximum element in constructed Array with sum divisible by K Last Updated : 27 Aug, 2021 Comments Improve Suggest changes Like Article Like Report Given two integers N and K, the task is to find the smallest value for maximum element of an array of size N consisting of positive integers whose sum of elements is divisible by K. Examples: Input: N = 4, K = 3Output: 2Explanation: Let the array be [2, 2, 1, 1]. Here, sum of elements of this array is divisible by K=3, and maximum element is 2. Input: N = 3, K = 5Output: 2 Approach: To find the smallest maximum of an array of size N and having sum divisible by K, try to create an array with the minimum sum possible. The minimum sum of N elements (each having a value greater than 0) that is divisible by K is: sum = K * ceil(N/K)Now, if the sum is divisible by N then the maximum element will be sum/N otherwise it is (sum/N + 1). Below is the implementation of above approach. C++ // C++ program for the above approach. #include <iostream> using namespace std; // Function to find smallest maximum number // in an array whose sum is divisible by K. int smallestMaximum(int N, int K) { // Minimum possible sum possible // for an array of size N such that its // sum is divisible by K int sum = ((N + K - 1) / K) * K; // If sum is not divisible by N if (sum % N != 0) return (sum / N) + 1; // If sum is divisible by N else return sum / N; } // Driver code. int main() { int N = 4; int K = 3; cout << smallestMaximum(N, K) << endl; return 0; } Java // Java program for the above approach import java.io.*; import java.util.*; class GFG{ // Function to find smallest maximum number // in an array whose sum is divisible by K. static int smallestMaximum(int N, int K) { // Minimum possible sum possible // for an array of size N such that its // sum is divisible by K int sum = ((N + K - 1) / K) * K; // If sum is not divisible by N if (sum % N != 0) return (sum / N) + 1; // If sum is divisible by N else return sum / N; } // Driver code public static void main(String args[]) { int N = 4; int K = 3; System.out.println(smallestMaximum(N, K)); } } // This code is contributed by code_hunt. Python3 # python program for the above approach. # Function to find smallest maximum number # in an array whose sum is divisible by K. def smallestMaximum(N,K): # Minimum possible sum possible # for an array of size N such that its # sum is divisible by K sum = ((N + K - 1) // K) * K # If sum is not divisible by N if (sum % N != 0): return (sum // N) + 1 # If sum is divisible by N else: return sum // N # Driver code. if __name__ == "__main__": N = 4 K = 3 print(smallestMaximum(N, K)) # This code is contributed by anudeep23042002. C# // C# program for the above approach. using System; using System.Collections.Generic; class GFG{ // Function to find smallest maximum number // in an array whose sum is divisible by K. static int smallestMaximum(int N, int K) { // Minimum possible sum possible // for an array of size N such that its // sum is divisible by K int sum = ((N + K - 1) / K) * K; // If sum is not divisible by N if (sum % N != 0) return (sum / N) + 1; // If sum is divisible by N else return sum / N; } // Driver code. public static void Main() { int N = 4; int K = 3; Console.Write(smallestMaximum(N, K)); } } // This code is contributed by SURENDRA_GANGWAR. JavaScript <script> // JavaScript Program to implement // the above approach // Function to find smallest maximum number // in an array whose sum is divisible by K. function smallestMaximum(N, K) { // Minimum possible sum possible // for an array of size N such that its // sum is divisible by K let sum = Math.floor((N + K - 1) / K) * K; // If sum is not divisible by N if (sum % N != 0) return Math.floor(sum / N) + 1; // If sum is divisible by N else return Math.floor(sum / N); } // Driver code. let N = 4; let K = 3; document.write(smallestMaximum(N, K)); // This code is contributed by Potta Lokesh </script> Output: 2 Time Complexity: O(1)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Minimize the maximum element in constructed Array with sum divisible by K K krishnumkhodke Follow Improve Article Tags : Mathematical DSA Arrays Practice Tags : ArraysMathematical Similar Reads Construct an Array of size N with sum divisible by K and array maximum is minimized Given integers N and K. The task is to construct an array of size N such that sum of all elements is divisible by K and the maximum element is as minimum as possible. Note: There can be many possible arrays. Printing any one of them is acceptable Examples: Input: N = 1, K = 5Output: 5Explanation: Su 8 min read Maximum sum of elements divisible by K from the given array Given an array of integers and a number K. The task is to find the maximum sum which is divisible by K from the given array.Examples: Input: arr[] = {3, 6, 5, 1, 8}, k = 3 Output: 18 Explanation: 18 is formed by the elements 3, 6, 1, 8.Input: arr = { 43, 1, 17, 26, 15 } , k = 16 Output: 32 Explanati 15 min read Minimum and Maximum element of an array which is divisible by a given number k Given an array, the task is to find the minimum and maximum elements in the array which are divisible by a given number k. Examples: Input: arr[] = {12, 1235, 45, 67, 1}, k=5 Output: Minimum = 45, Maximum = 1235 Input: arr[] = {10, 1230, 45, 67, 1}, k=10 Output: Minimum = 10, Maximum = 1230 Approach 7 min read Minimize divisions such that no Array element is divisible by K Given an array arr[] of size N and an integer K, the task is to find the minimum operations such that no variable is divisible by K. In each operation: Select any integer X from the array.Divide all occurrences of X by K. Examples: Input: arr[] = [2, 3, 4, 5], K = 2Output: 2Explanation:In the first 6 min read Construct a distinct elements array with given size, sum and element upper bound Given N, size of the original array, SUM total sum of all elements present in the array and K such that no element in array is greater than K, construct the original array where all elements in the array are unique. If there is no solution, print "Not Possible". Note: All elements should be positive 10 min read Like