Open In App

Maximize Kth largest element after splitting the given Array at most C times

Last Updated : 01 Nov, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

Given an array arr[] and two positive integers K and C, the task is to maximize the Kth maximum element obtained after splitting an array element arr[] into two parts(not necessarily an integer) C number of times. Print -1 if there doesn't exist Kth maximum element.

Note: It is compulsory to do splitting operation until the size of the array arr[] is ? K.

Examples:

Input: arr[] = {5, 8}, K = 1, C = 1
Output: 8.0
Explanation: There is no need to perform any operations. The finally array will be {8, 5} Hence 8.0 is the maximum achievable value.

Input: arr[] = {5, 9}, K = 3, C = 1
Output: 4.5
Explanation: The value 9 can be splitted as 4.5 and 4.5. The final array will be {5, 4.5, 4.5} where the 3rd value is 4.5 which is maximum achievable.

Approach: The given problem can be solved by using Binary Search on the answer. Follow the steps below to solve the given problem.

  • Initialize two variables, say low and high as 0 and 109 respectively that represents the range where Binary Search can be performed.
  • Perform the Binary Search using the following steps:
    • Find the value of mid as (low + high)*0.5.
    • Traverse the given array arr[] and store the count of elements which is at least the value of mid in the variable, say A and also find the number of operations performed in the variable, say B.
    • If the value of (A ? K) and (B + C ? K) then update the value of low as mid. Otherwise, update the value of high as mid.
  • After completing the above steps, the value stored in the variable low is the resultant maximized Kth maximum element.

Below is the implementation of the above approach:

C++
// C++ program for the above approach

#include <bits/stdc++.h>
using namespace std;

// Function to find the K-th maximum
// element after upto C operations
double maxKth(int arr[], int N,
              int C, int K)
{
    // Check for the base case
    if (N + C < K) {
        return -1;
    }
    // Stores the count iterations of BS
    int iter = 300;

    // Create the left and right bounds
    // of binary search
    double l = 0, r = 1000000000.0;

    // Perform binary search
    while (iter--) {

        // Find the value of mid
        double mid = (l + r) * 0.5;
        double a = 0;
        double b = 0;

        // Traverse the array
        for (int i = 0; i < N; i++) {
            a += int((double)arr[i] / mid);
            if ((double)arr[i] >= mid) {
                b++;
            }
        }

        // Update the ranges
        if (a >= K && b + C >= K) {
            l = mid;
        }
        else {
            r = mid;
        }
    }

    // Return the maximum value obtained
    return l;
}

// Driver Code
int main()
{
    int arr[] = { 5, 8 };
    int K = 1, C = 1;
    int N = sizeof(arr) / sizeof(arr[0]);

    cout << maxKth(arr, N, C, K);

    return 0;
}
Java
// Java program for the above approach
import java.io.*;

class GFG
{

    // Function to find the K-th maximum
    // element after upto C operations
    static double maxKth(int arr[], int N, int C, int K)
    {
      
        // Check for the base case
        if (N + C < K) {
            return -1;
        }
      
        // Stores the count iterations of BS
        int iter = 300;

        // Create the left and right bounds
        // of binary search
        double l = 0, r = 1000000000.0;

        // Perform binary search
        while (iter-- > 0) {

            // Find the value of mid
            double mid = (l + r) * 0.5;
            double a = 0;
            double b = 0;

            // Traverse the array
            for (int i = 0; i < N; i++) {
                a += (int)((double)arr[i] / mid);
                if ((double)arr[i] >= mid) {
                    b++;
                }
            }

            // Update the ranges
            if (a >= K && b + C >= K) {
                l = mid;
            }
            else {
                r = mid;
            }
        }

        // Return the maximum value obtained
        return l;
    }

    // Driver Code
    public static void main(String[] args)
    {
        int arr[] = { 5, 8 };
        int K = 1, C = 1;
        int N = arr.length;

        System.out.println(maxKth(arr, N, C, K));
    }
}

// This code is contributed by Dharanendra L V.
Python3
# Python Program to implement
# the above approach

# Function to find the K-th maximum
# element after upto C operations
def maxKth(arr, N, C, K):

    # Check for the base case
    if (N + C < K):
        return -1
    
    # Stores the count iterations of BS
    iter = 300

    # Create the left and right bounds
    # of binary search
    l = 0
    r = 1000000000.0

    # Perform binary search
    while (iter):
        iter = iter - 1
        # Find the value of mid
        mid = (l + r) * 0.5
        a = 0
        b = 0

        # Traverse the array
        for i in range(N) :
            a += arr[i] // mid
            if (arr[i] >= mid) :
                b += 1
            
        

        # Update the ranges
        if (a >= K and b + C >= K) :
                l = mid
        
        else :
                r = mid
    

    # Return the maximum value obtained
    return int(l)


# Driver Code
arr = [5, 8]
K = 1 
C = 1
N = len(arr)

print(maxKth(arr, N, C, K))

# This code is contributed by Saurabh Jaiswal
C#
// C# program for the above approach
using System;

class GFG
{

    // Function to find the K-th maximum
    // element after upto C operations
    static double maxKth(int []arr, int N, int C, int K)
    {
      
        // Check for the base case
        if (N + C < K) {
            return -1;
        }
      
        // Stores the count iterations of BS
        int iter = 300;

        // Create the left and right bounds
        // of binary search
        double l = 0, r = 1000000000.0;

        // Perform binary search
        while (iter-- > 0) {

            // Find the value of mid
            double mid = (l + r) * 0.5;
            double a = 0;
            double b = 0;

            // Traverse the array
            for (int i = 0; i < N; i++) {
                a += (int)((double)arr[i] / mid);
                if ((double)arr[i] >= mid) {
                    b++;
                }
            }

            // Update the ranges
            if (a >= K && b + C >= K) {
                l = mid;
            }
            else {
                r = mid;
            }
        }

        // Return the maximum value obtained
        return l;
    }

    // Driver Code
    public static void Main(String[] args)
    {
        int []arr = { 5, 8 };
        int K = 1, C = 1;
        int N = arr.Length;

        Console.Write(maxKth(arr, N, C, K));
    }
}

// This code is contributed by shivanisinghss2110
JavaScript
 <script>
        // JavaScript Program to implement
        // the above approach

        // Function to find the K-th maximum
        // element after upto C operations
        function maxKth(arr, N,
            C, K)
        {
        
            // Check for the base case
            if (N + C < K) {
                return -1;
            }
            // Stores the count iterations of BS
            let iter = 300;

            // Create the left and right bounds
            // of binary search
            let l = 0, r = 1000000000.0;

            // Perform binary search
            while (iter--) {

                // Find the value of mid
                let mid = (l + r) * 0.5;
                let a = 0;
                let b = 0;

                // Traverse the array
                for (let i = 0; i < N; i++) {
                    a += Math.floor(arr[i] / mid);
                    if (arr[i] >= mid) {
                        b++;
                    }
                }

                // Update the ranges
                if (a >= K && b + C >= K) {
                    l = mid;
                }
                else {
                    r = mid;
                }
            }

            // Return the maximum value obtained
            return l;
        }

        // Driver Code
        let arr = [5, 8];
        let K = 1, C = 1;
        let N = arr.length;

        document.write(maxKth(arr, N, C, K));

     // This code is contributed by Potta Lokesh

    </script>

Output: 
8

 

Time Complexity: O(N*log M)
Auxiliary Space: O(1)


Next Article

Similar Reads