Skip to content
Related Articles

Related Articles

Improve Article

Smallest number to make Array sum at most K by dividing each element

  • Difficulty Level : Medium
  • Last Updated : 14 May, 2021

Given an array arr[] of size N and a number K, the task is to find the smallest number M such that the sum of the array becomes lesser than or equal to the number K when every element of that array is divided by the number M.
Note: Each result of the division is rounded to the nearest integer greater than or equal to that element. For example: 10/3 = 4 and 6/2 = 3

Examples:

Input: arr[] = {2, 3, 4, 9}, K = 6 
Output:
Explanation: 
When every element is divided by 4- 2/4 + 3/4 + 4/4 + 9/4 = 1 + 1 + 1 + 3 = 6 
When every element is divided by 3- 2/3 + 3/3 + 4/3 + 9/3 = 1 + 1 + 2 + 3 = 7 which is greater than K. 
Hence, the smallest integer which makes the sum less than or equal to K = 6 is 4. 
Input: arr[] = {5, 6, 7, 8}, K = 4 
Output: 8  

Naive Approach: The naive approach for this problem is to start from 1 and for every number, divide every element in the array and check if the sum is less than or equal to K. The first number at which this condition satisfies is the required answer. 
Time complexity: O(N * M), where M is the number to be found and N is the size of the array. 

Efficient Approach: The idea is to use the concept of Binary Search.  



  1. Input the array.
  2. On assuming that the maximum possible answer is 109, initialize the max as 109 and the min as 1.
  3. Perform the Binary Search on this range and for every number, check if the sum is less than or equal to K.
  4. If the sum is less than K, then an answer might exist for a number that is smaller than this. So, continue and check for the numbers less than that counter.
  5. If the sum is greater than K, then the number M is greater than the current counter. So, continue and check for the numbers greater than that counter.

Below is the implementation of the above approach:  

C++




// C++ program to find the smallest
// number such that the sum of the
// array becomes less than or equal
// to K when every element of the
// array is divided by that number
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the smallest
// number such that the sum of the
// array becomes less than or equal
// to K when every element of the
// array is divided by that number
int findMinDivisor(int arr[], int n, int limit)
{
    // Binary search between 1 and 10^9
    int low = 0, high = 1e9;
    while (low < high) {
        int mid = (low + high) / 2;
        int sum = 0;
 
        // Calculating the new sum after
        // dividing every element by mid
        for (int i = 0; i < n; i++) {
            sum += ceil((double)arr[i]
                        / (double)mid);
        }
 
        // If after dividing by mid,
        // if the new sum is less than or
        // equal to limit move low to mid+1
        if (sum <= limit)
            high = mid;
        else
 
            // Else, move mid + 1 to high
            low = mid + 1;
    }
 
    // Returning the minimum number
    return low;
}
 
// Driver code
int main()
{
    int arr[] = { 2, 3, 4, 9 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    int K = 6;
 
    cout << findMinDivisor(arr, N, K);
}

Java




// Java program to find the smallest
// number such that the sum of the
// array becomes less than or equal
// to K when every element of the
// array is divided by that number
import java.util.*;
 
class GFG{
 
// Function to find the smallest
// number such that the sum of the
// array becomes less than or equal
// to K when every element of the
// array is divided by that number
static int findMinDivisor(int arr[],
                          int n, int limit)
{
     
    // Binary search between 1 and 10^9
    int low = 0, high = 1000000000;
     
    while (low < high)
    {
        int mid = (low + high) / 2;
        int sum = 0;
     
        // Calculating the new sum after
        // dividing every element by mid
        for(int i = 0; i < n; i++)
        {
           sum += Math.ceil((double) arr[i] /
                            (double) mid);
        }
     
        // If after dividing by mid,
        // if the new sum is less than or
        // equal to limit move low to mid+1
        if (sum <= limit)
            high = mid;
        else
         
            // Else, move mid + 1 to high
            low = mid + 1;
    }
 
    // Returning the minimum number
    return low;
}
 
// Driver Code
public static void main(String args[])
{
    int arr[] = { 2, 3, 4, 9 };
    int N = arr.length;
    int K = 6;
 
    System.out.println(
           findMinDivisor(arr, N, K));
}
}
 
// This code is contributed by rutvik_56

Python3




# Python3 program to find the smallest
# number such that the sum of the
# array becomes less than or equal
# to K when every element of the
# array is divided by that number
from math import ceil
 
# Function to find the smallest
# number such that the sum of the
# array becomes less than or equal
# to K when every element of the
# array is divided by that number
def findMinDivisor(arr, n, limit):
     
    # Binary search between 1 and 10^9
    low = 0
    high = 10 ** 9
     
    while (low < high):
        mid = (low + high) // 2
        sum = 0
 
        # Calculating the new sum after
        # dividing every element by mid
        for i in range(n):
            sum += ceil(arr[i] / mid)
 
        # If after dividing by mid,
        # if the new sum is less than or
        # equal to limit move low to mid+1
        if (sum <= limit):
            high = mid
        else:
 
            # Else, move mid + 1 to high
            low = mid + 1
 
    # Returning the minimum number
    return low
 
# Driver code
if __name__ == '__main__':
     
    arr= [ 2, 3, 4, 9 ]
    N = len(arr)
    K = 6
     
    print(findMinDivisor(arr, N, K))
 
# This code is contributed by mohit kumar 29

C#




// C# program to find the smallest
// number such that the sum of the
// array becomes less than or equal
// to K when every element of the
// array is divided by that number
using System;
 
class GFG{
 
// Function to find the smallest
// number such that the sum of the
// array becomes less than or equal
// to K when every element of the
// array is divided by that number
static int findMinDivisor(int []arr, int n,
                          int limit)
{
     
    // Binary search between 1 and 10^9
    int low = 0, high = 1000000000;
     
    while (low < high)
    {
        int mid = (low + high) / 2;
        int sum = 0;
     
        // Calculating the new sum after
        // dividing every element by mid
        for(int i = 0; i < n; i++)
        {
           sum += (int)Math.Ceiling((double) arr[i] /
                                    (double) mid);
        }
         
        // If after dividing by mid,
        // if the new sum is less than or
        // equal to limit move low to mid+1
        if (sum <= limit)
        {
            high = mid;
        }
        else
        {
 
            // Else, move mid + 1 to high
            low = mid + 1;
        }
    }
     
    // Returning the minimum number
    return low;
}
 
// Driver Code
public static void Main(String []args)
{
    int []arr = { 2, 3, 4, 9 };
    int N = arr.Length;
    int K = 6;
 
    Console.WriteLine(findMinDivisor(arr, N, K));
}
}
 
// This code is contributed by 29AjayKumar

Javascript




<script>
// Program  program to find the smallest
// number such that the sum of the
// array becomes less than or equal
// to K when every element of the
// array is divided by that number
 
// Function to find the smallest
// number such that the sum of the
// array becomes less than or equal
// to K when every element of the
// array is divided by that number
function findMinDivisor(arr, n, limit)
{
       
    // Binary search between 1 and 10^9
    let low = 0, high = 1000000000;
       
    while (low < high)
    {
        let mid = Math.floor((low + high) / 2);
        let sum = 0;
       
        // Calculating the new sum after
        // dividing every element by mid
        for(let i = 0; i < n; i++)
        {
           sum += Math.ceil( arr[i] / mid);
        }
       
        // If after dividing by mid,
        // if the new sum is less than or
        // equal to limit move low to mid+1
        if (sum <= limit)
            high = mid;
        else
           
            // Else, move mid + 1 to high
            low = mid + 1;
    }
   
    // Returning the minimum number
    return low;
}
 
// Driver Code
     
       let arr = [ 2, 3, 4, 9 ];
    let N = arr.length;
    let K = 6;
   
    document.write(
           findMinDivisor(arr, N, K));
            
</script>
Output: 
4

 

Time Complexity: O(N * 30), where N is the size of the array because finding any number between 1 and 109 takes at most 30 operations in binary search.
 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :