Open In App

Smallest positive integer that divides all array elements to generate quotients with sum not exceeding K

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Given an array arr[] of size N and a positive integer K, the task is to find the smallest positive integer such that the sum of remaining array elements obtained by dividing all array elements by that smallest positive integer does not exceed K

Note: Dividing an array element by the smallest positive integer must be of Ceil type.

Examples:

Input: arr[] = {1, 2, 5, 9}, K = 6 
Output:
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:
 

Naive Approach:The simplest approach to solve this problem is to find the largest element in the array, say Max, and iterate over the range [1, Max] using the variable i and check if the sum of the remaining array elements after dividing all the array elements by i is less than or equal to K or not. If found to be true, then print i

Time Complexity: O(N * Max), where Max is the largest element of the array. 
Auxiliary Space: O(1)

Efficient Approach: To optimize the above approach the idea is to use Binary search technique. Follow the steps below to solve the problem:

  • Initialize a variable, say Max, to store the largest element present in the array.
  • The value of the smallest positive integer which divides all the array elements to get the sum of remaining array elements less than or equal to K, must lie in the range [1, Max]. Therefore, apply binary search over the range [1, Max].
  • Initialize two variables, say left = 1 and right = Max, to store the range in which the value of the required output lies.
  • Check if it is possible to get the sum of the array elements less than or equal to K by dividing the array elements by (left + right) / 2 or not. If found to be true, then update right = (left + right) / 2.
  • Otherwise, update left = (left + right) /2.
  • Finally, print the value of left.

Below is the implementation of the above approach: 

C++




// C++ program to implement
// the above approach
 
#include <bits/stdc++.h>
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


Javascript




<script>
 
// Javascript program to implement
// the above approach
 
// Function to find the smallest positive integer
// that divides array elements to get the sum <= K
function findSmallestInteger(arr, N, K)
{
     
    // Stores minimum possible of the smallest
    // positive integer satisfying the condition
    var left = 1;
 
    // Stores maximum possible of the smallest
    // positive integer satisfying the condition
    var right = arr.reduce((a, b) => Math.max(a, b));
 
    // Apply binary search over
    // the range [left, right]
    while (left < right)
    {
         
        // Stores middle element
        // of left and right
        var mid = (left + right) / 2;
 
        // Stores sum of
        // array elements
        var sum = 0;
 
        // Traverse the array
        for(var i = 0; i < N; i++)
        {
             
            // Update sum
            sum += parseInt((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
var arr = [ 1, 2, 5, 9 ];
var N = arr.length;
var K = 6;
 
document.write(findSmallestInteger(arr, N, K));
 
// This code is contributed by importantly
 
</script>


Output: 

5

 

Time Complexity: O(N * log(Max)), where Max is the largest element of the array. 
Auxiliary Space: O(1)



Last Updated : 06 Jun, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads