Open In App

Split Array Elements to Make the Degree Lowest

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:

Below is the implementation of the above approach.




// C++ code to implement the approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the minimum of maximum
int largeElement(vector<int>& arr, int K)
{
    // Initilize a variable start = 1 and
    // end = maximum possible answer.
    int start = 1,
        end = *max_element(arr.begin(), arr.end());
 
    // Initilize a varaible result which
    // will store the answer
    int result = -1;
 
    // Do while start <= end
    while (start <= end) {
 
        // Calculate mid = (start + end) / 2;
        int mid = (start + end) >> 1;
 
        // Calculate maximum number of
        // oparation required to make
        // every element less than or
        // equal to mid.
        int operation = 0;
 
        // Iterate over the given array
        for (int i = 0; i < arr.size(); i++) {
 
            // Check if current element is
            // greater than mid, If true,
            // then calculate the operation
            // required to make this element
            // less than or equals to mid
            if (arr[i] > mid) {
                operation += ceil((double)arr[i] / mid) - 1;
            }
        }
 
        // Check if total operation
        // required to make every element
        // less than or equal to mid is
        // greater less than equal to K
        // If true, update the result and
        // move the end to mid - 1
        if (operation <= K) {
            result = mid;
            end = mid - 1;
        }
        else {
            start = mid + 1;
        }
    }
 
    // Return the result.
    return result;
}
 
// Driver code
int main()
{
 
    vector<int> arr = { 2, 4, 8, 2 };
    int K = 4;
 
    // Functions call
    cout << largeElement(arr, K);
 
    return 0;
}




/*code by Flutterfly */
import java.util.Arrays;
 
class Main {
     
    // Function to find the minimum of maximum
    static int largeElement(int[] arr, int K) {
        // Initialize a variable start = 1 and end = maximum possible answer.
        int start = 1;
        int end = Arrays.stream(arr).max().getAsInt();
 
        // Initialize a variable result which will store the answer.
        int result = -1;
 
        // Do while start <= end
        while (start <= end) {
            // Calculate mid = (start + end) / 2;
            int mid = (start + end) >>> 1; // Using unsigned right shift for mid calculation.
 
            // Calculate the maximum number of operations required to make every element
            // less than or equal to mid.
            int operation = 0;
 
            // Iterate over the given array
            for (int i = 0; i < arr.length; i++) {
                // Check if the current element is greater than mid.
                // If true, then calculate the operation required to make this element
                // less than or equal to mid.
                if (arr[i] > mid) {
                    operation += Math.ceil((double) arr[i] / mid) - 1;
                }
            }
 
            // Check if the total operation required to make every element
            // less than or equal to mid is greater than or equal to K.
            // If true, update the result and move the end to mid - 1.
            if (operation <= K) {
                result = mid;
                end = mid - 1;
            } else {
                start = mid + 1;
            }
        }
 
        // Return the result.
        return result;
    }
 
    // Driver code
    public static void main(String[] args) {
        int[] arr = {2, 4, 8, 2};
        int K = 4;
 
        // Function call
        System.out.println(largeElement(arr, K));
    }
}




import math
 
# Function to find the minimum of maximum
def large_element(arr, K):
    # Initialize a variable start = 1 and
    # end = maximum possible answer.
    start = 1
    end = max(arr)
 
    # Initialize a variable result which
    # will store the answer
    result = -1
 
    # Do while start <= end
    while start <= end:
        # Calculate mid = (start + end) / 2;
        mid = (start + end) // 2
 
        # Calculate maximum number of
        # operations required to make
        # every element less than or
        # equal to mid.
        operation = 0
 
        # Iterate over the given array
        for i in range(len(arr)):
            # Check if the current element is
            # greater than mid. If true,
            # then calculate the operation
            # required to make this element
            # less than or equal to mid
            if arr[i] > mid:
                operation += math.ceil(arr[i] / mid) - 1
 
        # Check if total operations
        # required to make every element
        # less than or equal to mid is
        # less than or equal to K
        # If true, update the result and
        # move the end to mid - 1
        if operation <= K:
            result = mid
            end = mid - 1
        else:
            start = mid + 1
 
    # Return the result.
    return result
 
# Driver code
if __name__ == "__main__":
    arr = [2, 4, 8, 2]
    K = 4
 
    # Function call
    print(large_element(arr, K))




//code by Flutterfly
using System;
using System.Linq;
 
class Program
{
    // Function to find the minimum of maximum
    static int LargeElement(int[] arr, int K)
    {
        // Initialize a variable start = 1 and end = maximum possible answer.
        int start = 1;
        int end = arr.Max();
 
        // Initialize a variable result which will store the answer.
        int result = -1;
 
        // Do while start <= end
        while (start <= end)
        {
            // Calculate mid = (start + end) / 2;
            int mid = (start + end) / 2;
 
            // Calculate the maximum number of operations required to make every element
            // less than or equal to mid.
            int operation = 0;
 
            // Iterate over the given array
            foreach (int element in arr)
            {
                // Check if the current element is greater than mid.
                // If true, then calculate the operation required to make this element
                // less than or equal to mid.
                if (element > mid)
                {
                    operation += (int)Math.Ceiling((double)element / mid) - 1;
                }
            }
 
            // Check if the total operation required to make every element
            // less than or equal to mid is greater than or equal to K.
            // If true, update the result and move the end to mid - 1.
            if (operation <= K)
            {
                result = mid;
                end = mid - 1;
            }
            else
            {
                start = mid + 1;
            }
        }
 
        // Return the result.
        return result;
    }
 
    // Driver code
    static void Main()
    {
        int[] arr = { 2, 4, 8, 2 };
        int K = 4;
 
        // Function call
        Console.WriteLine(LargeElement(arr, K));
    }
}




// Function to find the minimum of maximum
function largeElement(arr, K) {
    // Initialize a variable start = 1 and end = maximum possible answer.
    let start = 1;
    let end = Math.max(...arr);
 
    // Initialize a variable result which will store the answer.
    let result = -1;
 
    // Do while start <= end
    while (start <= end) {
        // Calculate mid = (start + end) / 2;
        let mid = Math.floor((start + end) / 2);
 
        // Calculate the maximum number of operations required to make every element
        // less than or equal to mid.
        let operation = 0;
 
        // Iterate over the given array
        for (let i = 0; i < arr.length; i++) {
            // Check if the current element is greater than mid.
            // If true, then calculate the operation required to make this element
            // less than or equal to mid.
            if (arr[i] > mid) {
                operation += Math.ceil(arr[i] / mid) - 1;
            }
        }
 
        // Check if the total operation required to make every element
        // less than or equal to mid is greater than or equal to K.
        // If true, update the result and move the end to mid - 1.
        if (operation <= K) {
            result = mid;
            end = mid - 1;
        } else {
            start = mid + 1;
        }
    }
 
    // Return the result.
    return result;
}
 
// Driver code
let arr = [2, 4, 8, 2];
let K = 4;
 
// Function call
console.log(largeElement(arr, K));

Output
2





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)


Article Tags :