Construct an Array of size N with sum divisible by K and array maximum is minimized
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> |
Output
2 1 1 2
Time Complexity: O(N)
Auxiliary Space: O(N)
Please Login to comment...