Find minimum number K such that sum of array after multiplication by K 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 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:



Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Output:

4

Time Complexity: O(N)
Space Complexity: O(1)

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.