给定一个大小为N的数组arr[]和一个数字K ,任务是找到最小的数字M ,当该数组的每个元素除以数字时,该数组的总和变得小于或等于数字K米。
注意:除法的每个结果都四舍五入为最接近的大于或等于该元素的整数。例如: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
Javascript
4
时间复杂度: O(N * 30) ,其中 N 是数组的大小,因为在二分搜索中查找 1 到 10 9之间的任何数字最多需要 30 次操作。
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live