Minimum K such that sum of array elements after division by K does not exceed S
Last Updated :
17 Apr, 2023
Given an array arr[] of N elements and an integer S. The task is to find the minimum number K such that the sum of the array elements does not exceed S after dividing all the elements by K.
Note: Consider integer division.
Examples:
Input: arr[] = {10, 7, 8, 10, 12, 19}, S = 27
Output: 3
After dividing by 3, the array becomes
{3, 2, 2, 3, 4, 6} and the new sum is 20.
Input: arr[] = {19, 17, 11, 10}, S = 40
Output: 2
Naive approach: Iterate for all values of K from 1 to the maximum element in the array plus one not maximum element because if we put k as maximum element then the sum will be one and what if the S is given to us as zero. So we iterate from k=1 to max element in the array plus one. and then sum up the array elements by dividing with K, if the sum does not exceed S then the current value will be the answer. The time complexity of this approach will be O(M * N) where M is the maximum element in the array.
C++
#include <iostream>
#include <numeric> // Required for std::accumulate
int FindK( int arr[], int n, int S)
{
int K = 1;
while ( true )
{
int sumArr = std::accumulate(arr, arr+n, 0, [=]( int a, int b) { return a + b/K; });
if (sumArr <= S)
{
return K;
}
K++;
}
}
int main()
{
int arr[] = {10, 7, 8, 10, 12, 19};
int n = sizeof (arr)/ sizeof (arr[0]);
int S = 27;
std::cout << FindK(arr, n, S) << std::endl;
return 0;
}
|
Java
import java.util.*;
public class Main {
public static int FindK( int arr[], int n, int S) {
int K = 1 ;
while ( true ) {
final int k = K;
int sumArr = Arrays.stream(arr).reduce(
0 ,
(a, b) -> a + b/k);
if (sumArr <= S) {
return K;
}
K++;
}
}
public static void main(String[] args) {
int arr[] = { 10 , 7 , 8 , 10 , 12 , 19 };
int n = arr.length;
int S = 27 ;
System.out.println(FindK(arr, n, S));
}
}
|
Python3
arr = [ 10 , 7 , 8 , 10 , 12 , 19 ]
S = 27
def find_k(arr, S):
K = 1
while True :
sum_arr = sum ([i / / K for i in arr])
if sum_arr < = S:
return K
K + = 1
print (find_k(arr, S))
|
Javascript
const arr = [10, 7, 8, 10, 12, 19];
const S = 27;
function find_k(arr, S) {
let K = 1;
while ( true ) {
let sum_arr = arr.reduce((acc, curr) => acc + Math.floor(curr / K), 0);
if (sum_arr <= S) {
return K;
}
K++;
}
}
console.log(find_k(arr, S));
|
C#
using System;
using System.Linq;
public class MinimumKValue
{
public static void Main()
{
int [] arr = {10, 7, 8, 10, 12, 19};
int S = 27;
Console.WriteLine(FindK(arr, S));
}
public static int FindK( int [] arr, int S)
{
int K = 1;
while ( true )
{
int sumArr = arr.Sum(i => i / K);
if (sumArr <= S)
{
return K;
}
K++;
}
}
}
|
Efficient approach: An efficient approach is to find the value of K by performing a binary search on the answer. Initiate a binary search on the value of K and a check is done inside it to see if the sum exceeds K then the binary search is performed on the second half or the first half accordingly.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findMinimumK( int a[], int n, int s)
{
int maximum = a[0];
for ( int i = 0; i < n; i++) {
maximum = max(maximum, a[i]);
}
int low = 1, high = maximum + 1;
int ans = high;
while (low <= high) {
int mid = (low + high) / 2;
int sum = 0;
for ( int i = 0; i < n; i++) {
sum += ( int )(a[i] / mid);
}
if (sum > s)
low = mid + 1;
else {
ans = min(ans, mid);
high = mid - 1;
}
}
return ans;
}
int main()
{
int a[] = { 10, 7, 8, 10, 12, 19 };
int n = sizeof (a) / sizeof (a[0]);
int s = 27;
cout << findMinimumK(a, n, s);
return 0;
}
|
Java
class GFG
{
static int findMinimumK( int a[],
int n, int s)
{
int maximum = a[ 0 ];
for ( int i = 0 ; i < n; i++)
{
maximum = Math.max(maximum, a[i]);
}
int low = 1 , high = maximum + 1 ;
int ans = high;
while (low <= high)
{
int mid = (low + high) / 2 ;
int sum = 0 ;
for ( int i = 0 ; i < n; i++)
{
sum += ( int )(a[i] / mid);
}
if (sum > s)
low = mid + 1 ;
else
{
ans = Math.min(ans, mid);
high = mid - 1 ;
}
}
return ans;
}
public static void main (String[] args)
{
int a[] = { 10 , 7 , 8 , 10 , 12 , 19 };
int n = a.length;
int s = 27 ;
System.out.println(findMinimumK(a, n, s));
}
}
|
Python3
def findMinimumK(a, n, s):
maximum = a[ 0 ]
for i in range (n):
maximum = max (maximum, a[i])
low = 1
high = maximum + 1
ans = high
while (low < = high):
mid = (low + high) / / 2
sum = 0
for i in range (n):
sum + = (a[i] / / mid)
if ( sum > s):
low = mid + 1
else :
ans = min (ans, mid)
high = mid - 1
return ans
a = [ 10 , 7 , 8 , 10 , 12 , 19 ]
n = len (a)
s = 27
print (findMinimumK(a, n, s))
|
Javascript
<script>
function findMinimumK(a , n , s) {
var maximum = a[0];
for (i = 0; i < n; i++) {
maximum = Math.max(maximum, a[i]);
}
var low = 1, high = maximum + 1;
var ans = high;
while (low <= high) {
var mid = parseInt((low + high) / 2);
var sum = 0;
for (i = 0; i < n; i++) {
sum += parseInt( (a[i] / mid));
}
if (sum > s)
low = mid + 1;
else {
ans = Math.min(ans, mid);
high = mid - 1;
}
}
return ans;
}
var a = [ 10, 7, 8, 10, 12, 19 ];
var n = a.length;
var s = 27;
document.write(findMinimumK(a, n, s));
</script>
|
C#
using System;
class GFG
{
static int findMinimumK( int []a,
int n, int s)
{
int maximum = a[0];
for ( int i = 0; i < n; i++)
{
maximum = Math.Max(maximum, a[i]);
}
int low = 1, high = maximum + 1;
int ans = high;
while (low <= high)
{
int mid = (low + high) / 2;
int sum = 0;
for ( int i = 0; i < n; i++)
{
sum += ( int )(a[i] / mid);
}
if (sum > s)
low = mid + 1;
else
{
ans = Math.Min(ans, mid);
high = mid - 1;
}
}
return ans;
}
public static void Main ()
{
int []a = { 10, 7, 8, 10, 12, 19 };
int n = a.Length;
int s = 27;
Console.WriteLine(findMinimumK(a, n, s));
}
}
|
Time Complexity: O(N*(log N)), N=Array length
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...