给定大小为N的数组arr []和一个正整数K ,任务是找到最小的正整数,以使通过将所有数组元素除以该最小的正整数而获得的剩余数组元素的总和不超过K。
注意:将数组元素除以最小的正整数必须属于Ceil类型。
例子:
Input: arr[] = {1, 2, 5, 9}, K = 6
Output: 5
Explanation:
Dividing all array elements by 5 modifies arr[] to {1, 1, 1, 2}.
Since the sum of the array elements is equal to 5 ( < K), the required output is 5.
Input: arr[]= {2, 3, 5, 7, 11}, K = 11
Output: 3
天真的方法:解决此问题的最简单方法是找到数组中最大的元素,例如Max ,并使用变量i遍历[1,Max]的范围,并检查除完所有元素后其余数组元素的总和i的数组元素是否小于或等于K。如果发现是真的,则打印i 。
时间复杂度: O(N * Max),其中Max是数组中的最大元素。
辅助空间: O(1)
高效的方法:为了优化上述方法,其思想是使用二进制搜索技术。请按照以下步骤解决问题:
- 初始化一个变量,例如Max ,以存储数组中存在的最大元素。
- 将所有数组元素相除以获得小于或等于K的其余数组元素之和的最小正整数的值必须在[1,Max]范围内。因此,在[1,Max]范围内应用二进制搜索。
- 初始化两个变量,例如left = 1和right = Max ,以存储所需输出值所在的范围。
- 检查是否可以通过将数组元素除以(left + right)/ 2来获得小于或等于K的数组元素之和。如果发现是真的,则更新right =(left + right)/ 2 。
- 否则,更新left =(left + right)/ 2 。
- 最后,打印left的值。
下面是上述方法的实现:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to find the smallest positive integer
// that divides array elements to get the sum <= K
int findSmallestInteger(int arr[], int N, int K)
{
// Stores minimum possible of the smallest
// positive integer satisfying the condition
int left = 1;
// Stores maximum possible of the smallest
// positive integer satisfying the condition
int right = *max_element(arr, arr + N);
// Apply binary search over
// the range [left, right]
while (left < right) {
// Stores middle element
// of left and right
int mid = (left + right) / 2;
// Stores sum of
// array elements
int sum = 0;
// Traverse the array
for (int i = 0; i < N; i++) {
// Update sum
sum += (arr[i] + mid - 1) / mid;
}
// If sum is greater than K
if (sum > K) {
// Update left
left = mid + 1;
}
else {
// Update right
right = mid;
}
}
return left;
}
// Driver Code
int main()
{
int arr[] = { 1, 2, 5, 9 };
int N = sizeof(arr) / sizeof(arr[0]);
;
int K = 6;
cout << findSmallestInteger(arr, N, K);
}
Java
// Java program to implement
// the above approach
import java.util.*;
class GFG{
// Function to find the smallest positive integer
// that divides array elements to get the sum <= K
static int findSmallestInteger(int arr[],
int N, int K)
{
// Stores minimum possible of the smallest
// positive integer satisfying the condition
int left = 1;
// Stores maximum possible of the smallest
// positive integer satisfying the condition
int right = Arrays.stream(arr).max().getAsInt();
// Apply binary search over
// the range [left, right]
while (left < right)
{
// Stores middle element
// of left and right
int mid = (left + right) / 2;
// Stores sum of
// array elements
int sum = 0;
// Traverse the array
for(int i = 0; i < N; i++)
{
// Update sum
sum += (arr[i] + mid - 1) / mid;
}
// If sum is greater than K
if (sum > K)
{
// Update left
left = mid + 1;
}
else
{
// Update right
right = mid;
}
}
return left;
}
// Driver code
public static void main(String[] args)
{
int[] arr = { 1, 2, 5, 9 };
int N = arr.length;
int K = 6;
System.out.println(findSmallestInteger(arr, N, K));
}
}
// This code is contributed by susmitakundugoaldanga
Python3
# Python3 program to implement
# the above approach
# Function to find the smallest positive integer
# that divides array elements to get the sum <= K
def findSmallestInteger(arr, N, K):
# Stores minimum possible of the smallest
# positive integer satisfying the condition
left = 1
# Stores maximum possible of the smallest
# positive integer satisfying the condition
right = max(arr)
# Apply binary search over
# the range [left, right]
while (left < right):
# Stores middle element
# of left and right
mid = (left + right) // 2
# Stores sum of
# array elements
sum = 0
# Traverse the array
for i in range(N):
# Update sum
sum += (arr[i] + mid - 1) // mid
# If sum is greater than K
if (sum > K):
# Update left
left = mid + 1
else:
# Update right
right = mid
return left
# Driver Code
if __name__ == '__main__':
arr = [1, 2, 5, 9]
N = len(arr)
K = 6
print(findSmallestInteger(arr, N, K))
# This code is contributed by mohit kumar 29
C#
// C# program to implement
// the above approach
using System;
using System.Linq;
class GFG{
// Function to find the smallest positive integer
// that divides array elements to get the sum <= K
static int findSmallestInteger(int[] arr,
int N, int K)
{
// Stores minimum possible of the smallest
// positive integer satisfying the condition
int left = 1;
// Stores maximum possible of the smallest
// positive integer satisfying the condition
int right = arr.Max();
// Apply binary search over
// the range [left, right]
while (left < right)
{
// Stores middle element
// of left and right
int mid = (left + right) / 2;
// Stores sum of
// array elements
int sum = 0;
// Traverse the array
for(int i = 0; i < N; i++)
{
// Update sum
sum += (arr[i] + mid - 1) / mid;
}
// If sum is greater than K
if (sum > K)
{
// Update left
left = mid + 1;
}
else
{
// Update right
right = mid;
}
}
return left;
}
// Driver Code
public static void Main()
{
int[] arr = { 1, 2, 5, 9 };
int N = arr.Length;
int K = 6;
Console.Write(findSmallestInteger(arr, N, K));
}
}
// This code is contributed by sanjoy_62
输出:
5
时间复杂度: O(N * log(Max)),其中Max是数组中最大的元素。
辅助空间: O(1)