给定一个由N 个元素组成的数组arr[]和一个整数K,任务是对数组最多执行K 个操作。在 one 操作中,将任何元素增加一个数组。在进行K 次这样的操作后找到最大中位数。
例子:
Input: arr[] = {1, 3, 4, 5}, K = 3
Output: 5
Explanation: Here we add two in the second element and one in the third element then we will get a maximum median. After k operation the array can become {1, 5, 5, 5}. So the maximum median we can make is ( 5 + 5 ) / 2 = 5, because here N is even.
Input: arr[] = {1, 3, 6, 4, 2}, K = 10
Output: 7
方法:
- 按升序对数组进行排序。
- 由于中位数是在左半部分进行操作的数组的中间元素,因此它将毫无价值,因为它不会增加中位数。
- 执行后半部分的操作,从第n/2个元素开始执行到最后的操作。
- 如果N是偶数,则从n/2元素开始执行操作直到结束。
- 使用二分搜索,我们将在执行K操作后检查任何数字是否可能作为中位数。
- 如果中位数是可能的,那么我们将检查下一个大于当前计算中位数的数字。否则,中位数的最后一个可能值就是所需的结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to check operation can be
// perform or not
bool possible(int arr[], int N,
int mid, int K)
{
int add = 0;
for (int i = N / 2 - (N + 1) % 2;
i < N; ++i) {
if (mid - arr[i] > 0) {
// Number of operation to
// perform s.t. mid is median
add += (mid - arr[i]);
if (add > K)
return false;
}
}
// If mid is median of the array
if (add <= K)
return true;
else
return false;
}
// Function to find max median
// of the array
int findMaxMedian(int arr[], int N,
int K)
{
// Lowest possible median
int low = 1;
int mx = 0;
for (int i = 0; i < N; ++i) {
mx = max(mx, arr[i]);
}
// Highest possible median
long long int high = K + mx;
while (low <= high) {
int mid = (high + low) / 2;
// Checking for mid is possible
// for the median of array after
// doing at most k operation
if (possible(arr, N, mid, K)) {
low = mid + 1;
}
else {
high = mid - 1;
}
}
if (N % 2 == 0) {
if (low - 1 < arr[N / 2]) {
return (arr[N / 2] + low - 1) / 2;
}
}
// Return the max possible ans
return low - 1;
}
// Driver Code
int main()
{
// Given array
int arr[] = { 1, 3, 6 };
// Given number of operation
int K = 10;
// Size of array
int N = sizeof(arr) / sizeof(arr[0]);
// Sort the array
sort(arr, arr + N);
// Function call
cout << findMaxMedian(arr, N, K);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to check operation can be
// perform or not
static boolean possible(int arr[], int N,
int mid, int K)
{
int add = 0;
for(int i = N / 2 - (N + 1) % 2;
i < N; ++i)
{
if (mid - arr[i] > 0)
{
// Number of operation to
// perform s.t. mid is median
add += (mid - arr[i]);
if (add > K)
return false;
}
}
// If mid is median of the array
if (add <= K)
return true;
else
return false;
}
// Function to find max median
// of the array
static int findMaxMedian(int arr[], int N,
int K)
{
// Lowest possible median
int low = 1;
int mx = 0;
for(int i = 0; i < N; ++i)
{
mx = Math.max(mx, arr[i]);
}
// Highest possible median
int high = K + mx;
while (low <= high)
{
int mid = (high + low) / 2;
// Checking for mid is possible
// for the median of array after
// doing at most k operation
if (possible(arr, N, mid, K))
{
low = mid + 1;
}
else
{
high = mid - 1;
}
}
if (N % 2 == 0)
{
if (low - 1 < arr[N / 2])
{
return (arr[N / 2] + low - 1) / 2;
}
}
// Return the max possible ans
return low - 1;
}
// Driver code
public static void main(String[] args)
{
// Given array
int arr[] = { 1, 3, 6 };
// Given number of operation
int K = 10;
// Size of array
int N = arr.length;
// Sort the array
Arrays.sort(arr);
// Function call
System.out.println(findMaxMedian(arr, N, K));
}
}
// This code is contributed by offbeat
Python3
# Python3 program for the above approach
# Function to check operation can be
# perform or not
def possible(arr, N, mid, K):
add = 0
for i in range(N // 2 - (N + 1) % 2, N):
if (mid - arr[i] > 0):
# Number of operation to
# perform s.t. mid is median
add += (mid - arr[i])
if (add > K):
return False
# If mid is median of the array
if (add <= K):
return True
else:
return False
# Function to find max median
# of the array
def findMaxMedian(arr, N,K):
# Lowest possible median
low = 1
mx = 0
for i in range(N):
mx = max(mx, arr[i])
# Highest possible median
high = K + mx
while (low <= high):
mid = (high + low) // 2
# Checking for mid is possible
# for the median of array after
# doing at most k operation
if (possible(arr, N, mid, K)):
low = mid + 1
else :
high = mid - 1
if (N % 2 == 0):
if (low - 1 < arr[N // 2]):
return (arr[N // 2] + low - 1) // 2
# Return the max possible ans
return low - 1
# Driver Code
if __name__ == '__main__':
# Given array
arr = [1, 3, 6]
# Given number of operation
K = 10
# Size of array
N = len(arr)
# Sort the array
arr = sorted(arr)
# Function call
print(findMaxMedian(arr, N, K))
# This code is contributed by Mohit Kumar
C#
// C# program for the above approach
using System;
class GFG{
// Function to check operation can be
// perform or not
static bool possible(int []arr, int N,
int mid, int K)
{
int add = 0;
for(int i = N / 2 - (N + 1) % 2;
i < N; ++i)
{
if (mid - arr[i] > 0)
{
// Number of operation to
// perform s.t. mid is median
add += (mid - arr[i]);
if (add > K)
return false;
}
}
// If mid is median of the array
if (add <= K)
return true;
else
return false;
}
// Function to find max median
// of the array
static int findMaxMedian(int []arr, int N,
int K)
{
// Lowest possible median
int low = 1;
int mx = 0;
for(int i = 0; i < N; ++i)
{
mx = Math.Max(mx, arr[i]);
}
// Highest possible median
int high = K + mx;
while (low <= high)
{
int mid = (high + low) / 2;
// Checking for mid is possible
// for the median of array after
// doing at most k operation
if (possible(arr, N, mid, K))
{
low = mid + 1;
}
else
{
high = mid - 1;
}
}
if (N % 2 == 0)
{
if (low - 1 < arr[N / 2])
{
return (arr[N / 2] + low - 1) / 2;
}
}
// Return the max possible ans
return low - 1;
}
// Driver code
public static void Main(string[] args)
{
// Given array
int []arr = { 1, 3, 6 };
// Given number of operation
int K = 10;
// Size of array
int N = arr.Length;
// Sort the array
Array.Sort(arr);
// Function call
Console.Write(findMaxMedian(arr, N, K));
}
}
// This code is contributed by rock_cool
Javascript
输出:
9
时间复杂度: O(N*log(K + M)) ,其中 M 是给定数组的最大元素。
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。