Maximize Kth largest element after splitting the given Array at most C times
Last Updated :
01 Nov, 2021
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++
#include <bits/stdc++.h>
using namespace std;
double maxKth( int arr[], int N,
int C, int K)
{
if (N + C < K) {
return -1;
}
int iter = 300;
double l = 0, r = 1000000000.0;
while (iter--) {
double mid = (l + r) * 0.5;
double a = 0;
double b = 0;
for ( int i = 0; i < N; i++) {
a += int (( double )arr[i] / mid);
if (( double )arr[i] >= mid) {
b++;
}
}
if (a >= K && b + C >= K) {
l = mid;
}
else {
r = mid;
}
}
return l;
}
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
import java.io.*;
class GFG
{
static double maxKth( int arr[], int N, int C, int K)
{
if (N + C < K) {
return - 1 ;
}
int iter = 300 ;
double l = 0 , r = 1000000000.0 ;
while (iter-- > 0 ) {
double mid = (l + r) * 0.5 ;
double a = 0 ;
double b = 0 ;
for ( int i = 0 ; i < N; i++) {
a += ( int )(( double )arr[i] / mid);
if (( double )arr[i] >= mid) {
b++;
}
}
if (a >= K && b + C >= K) {
l = mid;
}
else {
r = mid;
}
}
return l;
}
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));
}
}
|
Python3
def maxKth(arr, N, C, K):
if (N + C < K):
return - 1
iter = 300
l = 0
r = 1000000000.0
while ( iter ):
iter = iter - 1
mid = (l + r) * 0.5
a = 0
b = 0
for i in range (N) :
a + = arr[i] / / mid
if (arr[i] > = mid) :
b + = 1
if (a > = K and b + C > = K) :
l = mid
else :
r = mid
return int (l)
arr = [ 5 , 8 ]
K = 1
C = 1
N = len (arr)
print (maxKth(arr, N, C, K))
|
C#
using System;
class GFG
{
static double maxKth( int []arr, int N, int C, int K)
{
if (N + C < K) {
return -1;
}
int iter = 300;
double l = 0, r = 1000000000.0;
while (iter-- > 0) {
double mid = (l + r) * 0.5;
double a = 0;
double b = 0;
for ( int i = 0; i < N; i++) {
a += ( int )(( double )arr[i] / mid);
if (( double )arr[i] >= mid) {
b++;
}
}
if (a >= K && b + C >= K) {
l = mid;
}
else {
r = mid;
}
}
return l;
}
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));
}
}
|
Javascript
<script>
function maxKth(arr, N,
C, K)
{
if (N + C < K) {
return -1;
}
let iter = 300;
let l = 0, r = 1000000000.0;
while (iter--) {
let mid = (l + r) * 0.5;
let a = 0;
let b = 0;
for (let i = 0; i < N; i++) {
a += Math.floor(arr[i] / mid);
if (arr[i] >= mid) {
b++;
}
}
if (a >= K && b + C >= K) {
l = mid;
}
else {
r = mid;
}
}
return l;
}
let arr = [5, 8];
let K = 1, C = 1;
let N = arr.length;
document.write(maxKth(arr, N, C, K));
</script>
|
Time Complexity: O(N*log M)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...