Find minimum number K such that sum of array after multiplication by K exceed S Last Updated : 23 Mar, 2021 Comments Improve Suggest changes Like Article Like Report Given an array arr[] of N elements and an integer S, the task is to find the minimum number K such that the sum of the array elements does not exceed S after multiplying all the elements by K.Examples: Input: arr[] = { 1 }, S = 50 Output: 51 Explanation: The sum of array elements is 1. Now the multiplication of 1 with 51 gives 51 which is > 50. Hence the minimum value of K is 51.Input: arr[] = { 10, 7, 8, 10, 12, 19 }, S = 200 Output: 4 Explanation: The sum of array elements is 66. Now the multiplication of 66 with 4 gives 256 > 200. Hence the minimum value of K is 4. Approach: Find the sum of all elements of the array, store it in a variable sum.Take the ceil division of (S + 1) with sum. This will be the required minimum value of K. Below is the implementation of the above approach: C++ // C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the minimum value of k // that satisfies the given condition int findMinimumK(int a[], int n, int S) { // store sum of array elements int sum = 0; // Calculate the sum after for (int i = 0; i < n; i++) { sum += a[i]; } // return minimum possible K return ceil(((S + 1) * 1.0) / (sum * 1.0)); } // Driver code int main() { int a[] = { 10, 7, 8, 10, 12, 19 }; int n = sizeof(a) / sizeof(a[0]); int S = 200; cout << findMinimumK(a, n, S); return 0; } Java // Java implementation of the approach import java.io.*; import java.lang.Math; class GFG { // Function to return the minimum value of k // that satisfies the given condition static int findMinimumK(int a[], int n, int S) { // Store sum of array elements int sum = 0; // Calculate the sum after for (int i = 0; i < n; i++) { sum += a[i]; } // Return minimum possible K return (int) Math.ceil(((S + 1) * 1.0) / (sum * 1.0)); } // Driver code public static void main(String[] args) { int a[] = { 10, 7, 8, 10, 12, 19 }; int n = a.length; int S = 200; System.out.print(findMinimumK(a, n, S)); } } // This code is contributed by shivanisinghss2110 Python3 # Python3 implementation of the approach import math # Function to return the minimum value of k # that satisfies the given condition def findMinimumK(a, n, S) : # store sum of array elements sum = 0 # Calculate the sum after for i in range(0,n): sum += a[i] # return minimum possible K return math.ceil(((S + 1) * 1.0)/(sum * 1.0)) # Driver code a = [ 10, 7, 8, 10, 12, 19 ] n = len(a) s = 200 print(findMinimumK(a, n, s)) # This code is contributed by Sanjit_Prasad C# // C# implementation of the approach using System; class GFG { // Function to return the minimum value of k // that satisfies the given condition static int findMinimumK(int []a, int n, int S) { // Store sum of array elements int sum = 0; // Calculate the sum after for(int i = 0; i < n; i++) { sum += a[i]; } // Return minimum possible K return (int) Math.Ceiling(((S + 1) * 1.0) / (sum * 1.0)); } // Driver code public static void Main(String[] args) { int []a = { 10, 7, 8, 10, 12, 19 }; int n = a.Length; int S = 200; Console.Write(findMinimumK(a, n, S)); } } // This code is contributed by sapnasingh4991 JavaScript <script> // Javascript implementation of the approach // Function to return the minimum value of k // that satisfies the given condition function findMinimumK(a, n, S) { // store sum of array elements let sum = 0; // Calculate the sum after for (let i = 0; i < n; i++) { sum += a[i]; } // return minimum possible K return Math.ceil(((S + 1) * 1.0) / (sum * 1.0)); } let a = [ 10, 7, 8, 10, 12, 19 ]; let n = a.length; let S = 200; document.write(findMinimumK(a, n, S)); </script> Output: 4 Time Complexity: O(N) Space Complexity: O(1) Comment More infoAdvertise with us Next Article Find minimum number K such that sum of array after multiplication by K exceed S S Sanjit_Prasad Follow Improve Article Tags : Greedy Mathematical DSA Arrays Practice Tags : ArraysGreedyMathematical Similar Reads Minimum K such that sum of array elements after division by K does not exceed S Given an array arr[] of N elements and an integer S. The task is to find the minimum number K such that the sum of the array elements does not exceed S after dividing all the elements by K. Note: Consider integer division.Examples: Input: arr[] = {10, 7, 8, 10, 12, 19}, S = 27 Output: 3 After dividi 11 min read Make n using 1s and 2s with minimum number of terms multiple of k Given two positive integer n and k. n can be represented as the sum of 1s and 2s in many ways, using multiple numbers of terms. The task is to find the minimum number of terms of 1s and 2s use to make the sum n and also number of terms must be multiple of k. Print "-1", if no such number of terms ex 6 min read Find largest d in array such that a + b + c = d Given a set S (all distinct elements) of integers, find the largest d such that a + b + c = d where a, b, c, and d are distinct elements of S. Constraints: 1 ? number of elements in the set ? 1000 INT_MIN ? each element in the set ? INT_MAX Examples : Input : S[] = {2, 3, 5, 7, 12} Output : 12 Expla 15+ min read Maximize X such that sum of numbers in range [1, X] is at most K Given two integers N and an integer K, the task is to find the count of integers less than or equal to N, such that the sum of the natural numbers up to that integer is less than or equal to K. Examples: Input: N = 5, K = 10Output: 4Explanation: The integers 1, 2, 3, and 4 satisfy the condition. The 11 min read Maximize number of elements from Array with sum at most K Given an array A[] of N integers and an integer K, the task is to select the maximum number of elements from the array whose sum is at most K. Examples: Input: A[] = {1, 12, 5, 111, 200, 1000, 10}, K = 50 Output: 4 Explanation: Maximum number of selections will be 1, 12, 5, 10 that is 1 + 12 + 5 + 1 6 min read Like