构造一个大小为 N 的数组,其总和可被 K 整除,并且数组最大值最小化
给定整数N和K 。任务是构造一个大小为 N 的数组,使得所有元素的总和可以被 K 整除,并且最大元素尽可能小。
注意:可能有许多可能的数组。打印其中任何一个都是可以接受的
例子:
Input: N = 1, K = 5
Output: 5
Explanation: Sum of all elements = 5 and 5 is divisible by 5.
Input: N = 4, K = 3
Output: 2 1 1 2
Explanation: Sum of all elements = 6 and 6 is divisible by 3.
Input: N = 7, K = 6
Output: 2 2 2 2 1 1 2
方法:该解决方案是基于数组的和越小,最大元素越小的想法。按照步骤:
- 使用sum 等于因子乘以 K计算所需的结果数组的总和。
- 该因子等于 (N/K) 的底除法。
- 最后,计算数组的最大元素,即(sum/N) 的 ceil 除法。
下面是上述方法的实现。
C++
// C++ code to implement above approach
#include
using namespace std;
// Function to construct the array
void buildArray(int N, int K)
{
// arr to store solution
vector arr;
// Calculating factor
int cf = (N + K - 1) / K;
// Calculating sum
int sum = cf * K;
// Maximum element of array
int maxi = (sum + N - 1) / N;
// Initializing count variable from N
int c = N;
while (c * maxi >= sum) {
c--;
}
c--;
// Add maxi c times in arr
for (int i = 0; i < c; i++) {
arr.push_back(maxi);
}
// Out of N, c positions are filled
// in arr remaining position are
int rem_pos = N - c;
// Required remaining sum
int rem_sum = sum - (c * maxi);
// Finding last element
// by which arr should be filled
// on the remaining position
int last = (rem_sum / rem_pos);
// Pushing last element rem_pos-1 times
for (int i = 0; i < rem_pos - 1;
i++) {
arr.push_back(last);
}
// As 'last' element is floor division,
// so there would be possibility that
// last element would not satisfy
// given conditions
// If last element satisfies condition
if (last * (rem_pos) == rem_sum) {
arr.push_back(last);
}
else {
arr.push_back(rem_sum
- (last * (rem_pos - 1)));
}
// Printing the required array
for (auto it : arr) {
cout << it << ' ';
}
}
// Driver code
int main()
{
int N = 4, K = 3;
buildArray(N, K);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
// Function to construct the array
static void buildArray(int N, int K)
{
// arr to store solution
ArrayList arr = new ArrayList<>();
// Calculating factor
int cf = (N + K - 1) / K;
// Calculating sum
int sum = cf * K;
// Maximum element of array
int maxi = (sum + N - 1) / N;
// Initializing count variable from N
int c = N;
while (c * maxi >= sum) {
c--;
}
c--;
// Add maxi c times in arr
for (int i = 0; i < c; i++) {
arr.add(maxi);
}
// Out of N, c positions are filled
// in arr remaining position are
int rem_pos = N - c;
// Required remaining sum
int rem_sum = sum - (c * maxi);
// Finding last element
// by which arr should be filled
// on the remaining position
int last = (rem_sum / rem_pos);
// Pushing last element rem_pos-1 times
for (int i = 0; i < rem_pos - 1; i++) {
arr.add(last);
}
// As 'last' element is floor division,
// so there would be possibility that
// last element would not satisfy
// given conditions
// If last element satisfies condition
if (last * (rem_pos) == rem_sum) {
arr.add(last);
}
else {
arr.add(rem_sum - (last * (rem_pos - 1)));
}
// Printing the required array
for(int i = 0; i < arr.size(); i++){
System.out.print(arr.get(i) + " ");
}
}
public static void main (String[] args) {
int N = 4, K = 3;
buildArray(N, K);
}
}
// This code is contributed by hrithikgarg03188
Python3
# Python code to implement above approach
# Function to construct the array
def buildArray (N, K):
# arr to store solution
arr = [];
# Calculating factor
cf = (N + K - 1) // K;
# Calculating sum
sum = cf * K;
# Maximum element of array
maxi = (sum + N - 1) // N;
# Initializing count variable from N
c = N;
while (c * maxi >= sum):
c -= 1
c -= 1
# Add maxi c times in arr
for i in range(c):
arr.append(maxi);
# Out of N, c positions are filled
# in arr remaining position are
rem_pos = N - c;
# Required remaining sum
rem_sum = sum - (c * maxi);
# Finding last element
# by which arr should be filled
# on the remaining position
last = (rem_sum // rem_pos);
# Pushing last element rem_pos-1 times
for i in range(rem_pos - 1):
arr.append(last);
# As 'last' element is floor division,
# so there would be possibility that
# last element would not satisfy
# given conditions
# If last element satisfies condition
if (last * (rem_pos) == rem_sum):
arr.append(last);
else:
arr.append(rem_sum - (last * (rem_pos - 1)));
# Printing the required array
for it in arr:
print(it, end=" ");
# Driver code
N = 4
K = 3;
buildArray(N, K);
# This code is contributed by gfgking
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG
{
// Function to construct the array
static void buildArray(int N, int K)
{
// arr to store solution
List arr = new List();
// Calculating factor
int cf = (N + K - 1) / K;
// Calculating sum
int sum = cf * K;
// Maximum element of array
int maxi = (sum + N - 1) / N;
// Initializing count variable from N
int c = N;
while (c * maxi >= sum)
{
c--;
}
c--;
// Add maxi c times in arr
for (int i = 0; i < c; i++)
{
arr.Add(maxi);
}
// Out of N, c positions are filled
// in arr remaining position are
int rem_pos = N - c;
// Required remaining sum
int rem_sum = sum - (c * maxi);
// Finding last element
// by which arr should be filled
// on the remaining position
int last = (rem_sum / rem_pos);
// Pushing last element rem_pos-1 times
for (int i = 0; i < rem_pos - 1; i++)
{
arr.Add(last);
}
// As 'last' element is floor division,
// so there would be possibility that
// last element would not satisfy
// given conditions
// If last element satisfies condition
if (last * (rem_pos) == rem_sum)
{
arr.Add(last);
}
else
{
arr.Add(rem_sum - (last * (rem_pos - 1)));
}
// Printing the required array
for (int i = 0; i < arr.Count; i++)
{
Console.Write(arr[i] + " ");
}
}
public static void Main()
{
int N = 4, K = 3;
buildArray(N, K);
}
}
// This code is contributed by gfgking
Javascript
输出
2 1 1 2
时间复杂度: O(N)
辅助空间: O(N)