15. Smallest Divisor

โœ… GFG solution to the Smallest Divisor problem: find the smallest divisor such that sum of ceiling divisions is โ‰ค k using binary search. ๐Ÿš€

The problem can be found at the following link: ๐Ÿ”— Question Link

๐Ÿงฉ Problem Description

Given an integer array arr[] and an integer k (where k โ‰ฅ arr.length), find the smallest positive integer divisor such that the sum of the ceiling values of each element in arr[] divided by this divisor is less than or equal to k.

For a divisor d, the sum is calculated as: โŒˆarr[0]/dโŒ‰ + โŒˆarr[1]/dโŒ‰ + ... + โŒˆarr[n-1]/dโŒ‰ โ‰ค k

๐Ÿ“˜ Examples

Example 1

Input: arr[] = [1, 2, 5, 9], k = 6
Output: 5
Explanation: 5 is the smallest divisor having sum of quotients:
โŒˆ1/5โŒ‰ + โŒˆ2/5โŒ‰ + โŒˆ5/5โŒ‰ + โŒˆ9/5โŒ‰ = 1 + 1 + 1 + 2 = 5 โ‰ค 6

Example 2

Input: arr[] = [1, 1, 1, 1], k = 4
Output: 1
Explanation: 1 is the smallest divisor having sum of quotients:
โŒˆ1/1โŒ‰ + โŒˆ1/1โŒ‰ + โŒˆ1/1โŒ‰ + โŒˆ1/1โŒ‰ = 1 + 1 + 1 + 1 = 4 โ‰ค 4

๐Ÿ”’ Constraints

  • $1 \le \text{arr.size()} \le 10^5$

  • $1 \le \text{arr}[i] \le 10^6$

  • $\text{arr.size()} \le k \le 10^6$

โœ… My Approach

The optimal approach uses Binary Search on the answer space. Since we need the smallest divisor, we can search in the range [1, max(arr)]:

  1. Define Search Space:

    • Lower bound: l = 1 (smallest positive divisor)

    • Upper bound: h = max(arr) (largest possible needed divisor)

  2. Binary Search Logic:

    • For each mid value m, calculate the sum of ceiling divisions

    • Use the formula: โŒˆa/bโŒ‰ = (a + b - 1) / b for efficient ceiling calculation

    • If sum โ‰ค k, try smaller divisors (move right boundary)

    • If sum > k, need larger divisors (move left boundary)

  3. Ceiling Division Optimization:

    • Instead of using ceil(arr[i]/m), use (arr[i] + m - 1) / m

    • This avoids floating-point operations and is more efficient

  4. Termination:

    • When l >= h, we found the smallest valid divisor

๐Ÿ“ Time and Auxiliary Space Complexity

  • Expected Time Complexity: O(n * log(max_element)), where n is the array size. Binary search runs in O(log(max_element)) iterations, and each iteration requires O(n) time to calculate the sum of ceiling divisions.

  • Expected Auxiliary Space Complexity: O(1), as we only use constant extra space for variables like left, right, mid, and sum.

๐Ÿง‘โ€๐Ÿ’ป Code (C++)

class Solution {
public:
    int smallestDivisor(vector<int>& arr, int k) {
        int l = 1, h = *max_element(arr.begin(), arr.end()), n = arr.size();
        while (l < h) {
            int m = l + (h - l) / 2, s = 0;
            for (int i = 0; i < n; ++i)
                s += (arr[i] + m - 1) / m;
            if (s <= k) h = m;
            else l = m + 1;
        }
        return l;
    }
};
โšก View Alternative Approaches with Code and Analysis

๐Ÿ“Š 2๏ธโƒฃ Binary Search with Math Ceiling

๐Ÿ’ก Algorithm Steps:

  1. Use mathematical ceiling function for division.

  2. Standard binary search template.

  3. Explicit ceiling calculation.

class Solution {
public:
    int smallestDivisor(vector<int>& arr, int k) {
        int l = 1, h = *max_element(arr.begin(), arr.end()), r = h;
        while (l <= h) {
            int m = l + (h - l) / 2, s = 0;
            for (int x : arr) s += (x % m == 0) ? x / m : x / m + 1;
            if (s <= k) r = m, h = m - 1;
            else l = m + 1;
        }
        return r;
    }
};

๐Ÿ“ Complexity Analysis:

  • Time: โฑ๏ธ O(n * log(max_element))

  • Auxiliary Space: ๐Ÿ’พ O(1)

โœ… Why This Approach?

  • More readable ceiling division logic.

  • Classical binary search pattern.

๐Ÿ“Š 3๏ธโƒฃ Ternary Search Approach

๐Ÿ’ก Algorithm Steps:

  1. Divide search space into three parts instead of two.

  2. Compare function values at two mid points.

  3. Eliminate one-third of search space each iteration.

class Solution {
public:
    int smallestDivisor(vector<int>& arr, int k) {
        int l = 1, h = *max_element(arr.begin(), arr.end());
        while (h - l > 2) {
            int m1 = l + (h - l) / 3, m2 = h - (h - l) / 3, s1 = 0, s2 = 0;
            for (int x : arr) s1 += (x + m1 - 1) / m1, s2 += (x + m2 - 1) / m2;
            if (s1 <= k) h = m1;
            else if (s2 <= k) l = m1, h = m2;
            else l = m2;
        }
        for (int d = l; d <= h; d++) {
            int s = 0;
            for (int x : arr) s += (x + d - 1) / d;
            if (s <= k) return d;
        }
        return h;
    }
};

๐Ÿ“ Complexity Analysis:

  • Time: โฑ๏ธ O(n * logโ‚ƒ(max_element))

  • Auxiliary Space: ๐Ÿ’พ O(1)

โœ… Why This Approach?

  • Theoretically faster convergence than binary search.

  • Reduces search space by 1/3 each iteration.

๐Ÿ“Š 4๏ธโƒฃ Square Root Decomposition

๐Ÿ’ก Algorithm Steps:

  1. Divide array into โˆšn blocks.

  2. Precompute partial sums for blocks.

  3. Use binary search with block-wise calculation.

class Solution {
public:
    int smallestDivisor(vector<int>& arr, int k) {
        int n = arr.size(), b = sqrt(n) + 1;
        vector<vector<int>> blocks(b);
        for (int i = 0; i < n; i++) blocks[i / b].push_back(arr[i]);

        int l = 1, h = *max_element(arr.begin(), arr.end());
        while (l < h) {
            int m = l + (h - l) / 2, s = 0;
            for (auto& block : blocks)
                for (int x : block) s += (x + m - 1) / m;
            s <= k ? h = m : l = m + 1;
        }
        return l;
    }
};

๐Ÿ“ Complexity Analysis:

  • Time: โฑ๏ธ O(n * log(max_element))

  • Auxiliary Space: ๐Ÿ’พ O(n)

โœ… Why This Approach?

  • Better cache locality for large arrays.

  • Potential for parallelization.

๐Ÿ†š ๐Ÿ” Comparison of Approaches

๐Ÿš€ Approach

โฑ๏ธ Time Complexity

๐Ÿ’พ Space Complexity

โœ… Pros

โš ๏ธ Cons

๐Ÿ” Binary Search(Ultra-Optimized)

๐ŸŸข O(n * log(max))

๐ŸŸข O(1)

โšก Fastest runtime, minimal ops

๐Ÿงฎ Less readable

๐Ÿ”„ Math Ceiling

๐ŸŸข O(n * log(max))

๐ŸŸข O(1)

๐Ÿ”ง Clear division logic

๐Ÿข More operations per iteration

๐Ÿ”บ Ternary Search

๐ŸŸข O(n * logโ‚ƒ(max))

๐ŸŸข O(1)

๐Ÿš€ Faster theoretical convergence

๐Ÿงฎ Complex implementation

๐Ÿ“Š Sqrt Decomposition

๐ŸŸข O(n * log(max))

๐Ÿ”ธ O(n)

๐ŸŽ๏ธ Better cache locality

๐Ÿ’พ Extra space overhead

๐Ÿ† Best Choice Recommendation

๐ŸŽฏ Scenario

๐ŸŽ–๏ธ Recommended Approach

๐Ÿ”ฅ Performance Rating

โšก Maximum performance, large datasets

๐Ÿฅ‡ Binary Search

โ˜…โ˜…โ˜…โ˜…โ˜…

๐Ÿ”ง Code clarity with good performance

๐Ÿฅˆ Math Ceiling

โ˜…โ˜…โ˜…โ˜…โ˜†

๐Ÿ“Š Theoretical optimization

๐Ÿฅ‰ Ternary Search

โ˜…โ˜…โ˜…โ˜…โ˜†

๐ŸŽ๏ธ Cache-sensitive large arrays

๐Ÿ… Sqrt Decomposition

โ˜…โ˜…โ˜…โ˜†โ˜†

๐Ÿง‘โ€๐Ÿ’ป Code (Java)

class Solution {
    int smallestDivisor(int[] arr, int k) {
        int l = 1, h = Arrays.stream(arr).max().getAsInt();
        while (l < h) {
            int m = l + (h - l) / 2, s = 0;
            for (int x : arr)
                s += (x + m - 1) / m;
            if (s <= k) h = m;
            else l = m + 1;
        }
        return l;
    }
}

๐Ÿ Code (Python)

class Solution:
    def smallestDivisor(self, arr, k):
        l, h = 1, max(arr)
        while l < h:
            m, s = l + (h - l) // 2, 0
            for x in arr:
                s += (x + m - 1) // m
            if s <= k:
                h = m
            else:
                l = m + 1
        return l

๐Ÿง  Contribution and Support

For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: ๐Ÿ“ฌ Any Questions?. Let's make this learning journey more collaborative!

โญ If you find this helpful, please give this repository a star! โญ


๐Ÿ“Visitor Count

Visitor counter

Last updated