Split Array Elements to Make the Degree Lowest
Last Updated :
18 Dec, 2023
Given an array arr[] having N elements, and a positive integer K. The degree of the array is defined by the value of the largest element in the array. You can perform an operation on array elements that split any element arr[i] into two parts such that their sum remains equal to arr[i]. The task is to perform the given operation at most K times to make the degree of the given array lowest.
Examples:
Input: arr = {11}, K = 2
Output: 4
Explanation: Operation 1: Split element 11 into {7, 4} then the array becomes {7,4}.
Operation 2: Split element 7 into {4, 3} then the array becomes {4,3,4}.
So, the lowest degree of arr[] after performing at most K operations is 4.
Input: arr = {2, 4, 8, 2}, K = 4
Output: 2
Split Array Elements to Make the Degree Lowest using Binary Search
Initilze low with minimum possible answer and high with maximum possible answer, then calculate the middle value mid = (low+ high) /2 and check if it is possible to make every element less than or equals to mid in at most K operations. If it is possible, update the result and shift high of range to mid – 1. Otherwise, shift low of range to mid + 1.
Step-by-step approach:
- Initialize a variable low = 1 and high = maximum possible answer.
- Initialize a variable result that will store the answer
- While low ≤ high
- Calculate mid = (low + high) / 2
- Calculate the maximum number of operations required to make every element less than or equal to mid.
-
- Check if the total operation required to make every element less than or equal to mid is greater or less than equal to K
- If true, update the result and move the high to mid – 1
- Otherwise, move the low to mid + 1.
- Return the result.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int largeElement(vector< int >& arr, int K)
{
int start = 1,
end = *max_element(arr.begin(), arr.end());
int result = -1;
while (start <= end) {
int mid = (start + end) >> 1;
int operation = 0;
for ( int i = 0; i < arr.size(); i++) {
if (arr[i] > mid) {
operation += ceil (( double )arr[i] / mid) - 1;
}
}
if (operation <= K) {
result = mid;
end = mid - 1;
}
else {
start = mid + 1;
}
}
return result;
}
int main()
{
vector< int > arr = { 2, 4, 8, 2 };
int K = 4;
cout << largeElement(arr, K);
return 0;
}
|
Java
import java.util.Arrays;
class Main {
static int largeElement( int [] arr, int K) {
int start = 1 ;
int end = Arrays.stream(arr).max().getAsInt();
int result = - 1 ;
while (start <= end) {
int mid = (start + end) >>> 1 ;
int operation = 0 ;
for ( int i = 0 ; i < arr.length; i++) {
if (arr[i] > mid) {
operation += Math.ceil(( double ) arr[i] / mid) - 1 ;
}
}
if (operation <= K) {
result = mid;
end = mid - 1 ;
} else {
start = mid + 1 ;
}
}
return result;
}
public static void main(String[] args) {
int [] arr = { 2 , 4 , 8 , 2 };
int K = 4 ;
System.out.println(largeElement(arr, K));
}
}
|
Python3
import math
def large_element(arr, K):
start = 1
end = max (arr)
result = - 1
while start < = end:
mid = (start + end) / / 2
operation = 0
for i in range ( len (arr)):
if arr[i] > mid:
operation + = math.ceil(arr[i] / mid) - 1
if operation < = K:
result = mid
end = mid - 1
else :
start = mid + 1
return result
if __name__ = = "__main__" :
arr = [ 2 , 4 , 8 , 2 ]
K = 4
print (large_element(arr, K))
|
C#
using System;
using System.Linq;
class Program
{
static int LargeElement( int [] arr, int K)
{
int start = 1;
int end = arr.Max();
int result = -1;
while (start <= end)
{
int mid = (start + end) / 2;
int operation = 0;
foreach ( int element in arr)
{
if (element > mid)
{
operation += ( int )Math.Ceiling(( double )element / mid) - 1;
}
}
if (operation <= K)
{
result = mid;
end = mid - 1;
}
else
{
start = mid + 1;
}
}
return result;
}
static void Main()
{
int [] arr = { 2, 4, 8, 2 };
int K = 4;
Console.WriteLine(LargeElement(arr, K));
}
}
|
Javascript
function largeElement(arr, K) {
let start = 1;
let end = Math.max(...arr);
let result = -1;
while (start <= end) {
let mid = Math.floor((start + end) / 2);
let operation = 0;
for (let i = 0; i < arr.length; i++) {
if (arr[i] > mid) {
operation += Math.ceil(arr[i] / mid) - 1;
}
}
if (operation <= K) {
result = mid;
end = mid - 1;
} else {
start = mid + 1;
}
}
return result;
}
let arr = [2, 4, 8, 2];
let K = 4;
console.log(largeElement(arr, K));
|
Time Complexity: O(log2(max(arr)) * N), where max(arr) is the maximum element and N is the size of the given array.
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...