给定大小为N且数字为K的数组arr [] ,任务是找到最小的数字M ,以使当该数组的每个元素除以该数字时,该数组的总和小于或等于数字K。 M.
注意:除法的每个结果均四舍五入为大于或等于该元素的最接近整数。例如:10/3 = 4和6/2 = 3
例子:
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
天真的方法:这个问题的天真的方法是从1开始,对于每个数字,除以数组中的每个元素,然后检查总和是否小于或等于K。此条件满足的第一个数字是必需的答案。
时间复杂度: O(N * M) ,其中M是要找到的数字,N是数组的大小。
高效的方法:该想法是使用二进制搜索的概念。
- 输入数组。
- 假设最大可能答案为10 9 ,则将最大值初始化为10 9 ,将最小值初始化为1。
- 在此范围内执行二进制搜索,对于每个数字,检查总和是否小于或等于K。
- 如果总和小于K,则可能存在一个小于此数字的答案。因此,继续并检查小于该计数器的数字。
- 如果总和大于K,则数字M大于当前计数器。因此,继续并检查大于该计数器的数字。
下面是上述方法的实现:
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
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
4
时间复杂度: O(N * 30) ,其中N是数组的大小,因为找到1到10 9之间的任何数字在二进制搜索中最多需要30个操作。