 Open in App
Not now

# Minimize the maximum element in constructed Array with sum divisible by K

• Last Updated : 27 Aug, 2021

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 = 3
Output: 2
Explanation:
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 = 5
Output: 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 ``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

 ``

Output:

`2`

Time Complexity: O(1)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up