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

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: 4Â
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. Â
- Input the array.
- On assuming that the maximum possible answer is 109, initialize the max as 109 and the min as 1.
- Perform the Binary Search on this range and for every number, check if the sum is less than or equal to K.
- 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.
- 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 numberint 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 codeint 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 Codepublic 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 numberfrom 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 numberdef 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 codeif __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 Codepublic 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 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> |
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.
 Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!


