Skip to content
Related Articles

Related Articles

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

Improve Article
Save Article
Like Article
  • 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 <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)

 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!