Construct an Array of size N with sum divisible by K and array maximum is minimized
Last Updated :
24 Jan, 2022
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 = 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
Approach: The solution is based on the idea that the smaller the sum of the array the smaller the maximum element. Follow the steps:
- Calculate the required sum of resultant array by using sum equals factor times K.
- The factor equals floor division of (N/K).
- Finally, calculate the maximum element of the array which is ceil division of (sum/N).
Below is the implementation of the above approach.
C++
// C++ code to implement above approach
#include <bits/stdc++.h>
using namespace std;
// Function to construct the array
void buildArray(int N, int K)
{
// arr to store solution
vector<int> 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<Integer> 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<int> arr = new List<int>();
// 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
<script>
// JavaScript code to implement above approach
// Function to construct the array
const buildArray = (N, K) => {
// arr to store solution
arr = [];
// Calculating factor
let cf = parseInt((N + K - 1) / K);
// Calculating sum
let sum = cf * K;
// Maximum element of array
let maxi = parseInt((sum + N - 1) / N);
// Initializing count variable from N
let c = N;
while (c * maxi >= sum) {
c--;
}
c--;
// Add maxi c times in arr
for (let i = 0; i < c; i++) {
arr.push(maxi);
}
// Out of N, c positions are filled
// in arr remaining position are
let rem_pos = N - c;
// Required remaining sum
let rem_sum = sum - (c * maxi);
// Finding last element
// by which arr should be filled
// on the remaining position
let last = parseInt((rem_sum / rem_pos));
// Pushing last element rem_pos-1 times
for (let i = 0; i < rem_pos - 1;
i++) {
arr.push(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(last);
}
else {
arr.push(rem_sum
- (last * (rem_pos - 1)));
}
// Printing the required array
for (let it in arr) {
document.write(`${arr[it]} `);
}
}
// Driver code
let N = 4, K = 3;
buildArray(N, K);
// This code is contributed by rakeshsahni
</script>
Time Complexity: O(N)
Auxiliary Space: O(N)
Similar Reads
Minimize the maximum element in constructed Array with sum divisible by K 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
4 min read
Generate an N-length array having maximum element minimized and sum of array elements divisible by K Given two positive integers N and K, the task is to minimize the maximum element of the array formed such that the sum of array elements is positive and divisible by K. Examples: Input: N = 4, K = 50Output: 13Explanation The generated array is {12, 13, 12, 13}. Sum of the array is 50, which is divis
3 min read
Minimize maximum array element possible by at most K splits on the given array Given an array arr[] consisting of N positive integers and a positive integer K, the task is to minimize the maximum element present in the array by splitting at most K array elements into two numbers equal to their value. Examples: Input: arr[] = {2, 4, 8, 2}, K = 4Output: 2Explanation:Following se
9 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
Generate Array such that max is minimized and arr[i] != arr[j] when j is a multiple of i Given an integer N, the task is to generate an array arr[] having N positive integers such that arr[i] â arr[j] if j is divisible by i (1-based indexing is considered) such that the maximum value of the sequence is minimum among all possible sequences. Examples: Input: N = 3Output: 1 2 2Explanation:
5 min read