Smallest Divisor for Sum Constraint in Array Division
Last Updated :
28 Dec, 2023
Given an array of integers nums and an integer K, the task is to find the smallest positive integer divisor such that, upon dividing all the elements of the given array by it, the sum of the division’s result is less than or equal to the given integer K. Each result of the division is rounded to the nearest integer greater than or equal to that element.
Examples:
Input: A[] = [1, 2, 5, 9], K = 6
Output: 5
Explanation: This is because, if the divisor is 5, the sum will be 5 (1 + 1 + 1 + 2), which is less than 6.
Input: A[] = [1, 1, 1, 1], K = 3
Output: 1
Approach: To solve the problem follow the below steps:
- Initialize two variables left to 1 and right to the maximum element in the nums array.
- Perform a binary search within the range [left, right] to find the smallest divisor. Continue the search until left is less than right.
- Inside the binary search loop:
- Calculate the middle value mid as left + (right – left) / 2.
- b. Initialize a variable totalSum to keep track of the total sum of divisions for the current mid value.
- c. Loop through each element in the nums array and for each element num, calculate the division result as (num + mid – 1) / mid. This operation rounds up the division result to the nearest integer greater than or equal to the element.
- d. Add all these division results to totalSum.
- e. Check if totalSum is less than or equal to K. If it is, update right to mid. This means we can potentially find a smaller divisor. If not, update left to mid + 1.
- Once the binary search loop finishes, left will contain the smallest divisor that satisfies the condition. Return left as the result.
Below is the Code for the above approach:
C++
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int smallestDivisor(vector< int >& nums, int K)
{
int left = 1,
right = *max_element(nums.begin(), nums.end());
while (left < right) {
int mid = left + (right - left) / 2;
int totalSum = 0;
for ( int num : nums) {
totalSum += (num + mid - 1) / mid;
}
if (totalSum <= K) {
right = mid;
}
else {
left = mid + 1;
}
}
return left;
}
int main()
{
vector< int > A = { 1, 2, 5, 9 };
int K = 6;
int result = smallestDivisor(A, K);
cout << result << endl;
return 0;
}
|
Java
import java.util.*;
public class SmallestDivisor {
public static int smallestDivisor(ArrayList<Integer> nums, int K) {
int left = 1 ;
int right = Collections.max(nums);
while (left < right) {
int mid = left + (right - left) / 2 ;
int totalSum = 0 ;
for ( int num : nums) {
totalSum += (num + mid - 1 ) / mid;
}
if (totalSum <= K) {
right = mid;
} else {
left = mid + 1 ;
}
}
return left;
}
public static void main(String[] args) {
ArrayList<Integer> A = new ArrayList<>(Arrays.asList( 1 , 2 , 5 , 9 ));
int K = 6 ;
int result = smallestDivisor(A, K);
System.out.println(result);
}
}
|
Python3
import math
def smallestDivisor(nums, K):
left = 1
right = max (nums)
while left < right:
mid = left + (right - left) / / 2
total_sum = 0
for num in nums:
total_sum + = (num + mid - 1 ) / / mid
if total_sum < = K:
right = mid
else :
left = mid + 1
return left
if __name__ = = "__main__" :
A = [ 1 , 2 , 5 , 9 ]
K = 6
result = smallestDivisor(A, K)
print (result)
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
public class GFG
{
public static int SmallestDivisor(List< int > nums, int K)
{
int left = 1;
int right = nums.Max();
while (left < right)
{
int mid = left + (right - left) / 2;
int totalSum = 0;
foreach ( int num in nums)
{
totalSum += (num + mid - 1) / mid;
}
if (totalSum <= K)
{
right = mid;
}
else
{
left = mid + 1;
}
}
return left;
}
public static void Main( string [] args)
{
List< int > A = new List< int > { 1, 2, 5, 9 };
int K = 6;
int result = SmallestDivisor(A, K);
Console.WriteLine(result);
}
}
|
Javascript
function smallestDivisor(nums, K) {
let left = 1;
let right = Math.max(...nums);
while (left < right) {
let mid = left + Math.floor((right - left) / 2);
let totalSum = 0;
for (let num of nums) {
totalSum += Math.ceil(num / mid);
}
if (totalSum <= K) {
right = mid;
} else {
left = mid + 1;
}
}
return left;
}
const A = [1, 2, 5, 9];
const K = 6;
const result = smallestDivisor(A, K);
console.log(result);
|
Time Complexity:
Auxiliary Space:
Share your thoughts in the comments
Please Login to comment...