📌  相关文章
📜  最小化构造数组中的最大元素,总和可被 K 整除

📅  最后修改于: 2022-05-13 01:56:07.781000             🧑  作者: Mango

最小化构造数组中的最大元素,总和可被 K 整除

给定两个整数NK ,任务是找到一个大小为N的数组的最大元素的最小值,该数组由元素之和可被K整除的正整数组成。

例子:

方法:要找到大小为 N 且总和可被 K 整除的数组的最小最大值,请尝试创建一个总和可能最小的数组。

  • 能被 K 整除的 N 个元素(每个元素的值都大于 0 )的最小总和是:
sum = K * ceil(N/K)
  • 现在,如果总和可被 N 整除,则最大元素将为sum/N ,否则为(sum/N + 1)。

下面是上述方法的实现。

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



输出:
2

时间复杂度: O(1)
辅助空间: O(1)