📌  相关文章
📜  精确删除k个元素后,数组的最大可能中间元素

📅  最后修改于: 2021-05-08 16:35:11             🧑  作者: Mango

给定一个大小为n且数字为k的整数数组。如果索引是基于1的,则数组的中间元素是索引(n + 1)/ 2的元素,如果n为奇数,否则为n /2。任务是以这种方式从数组中准确删除k个元素精简数组的中间元素尽可能地大。精确删除k个元素后,找到数组的最大可能中间元素。
例子:

Input :
n = 5, k = 2
arr[] = {9, 5, 3, 7, 10};
Output : 7

Input :
n = 9, k = 3
arr[] = {2, 4, 3, 9, 5, 8, 7, 6, 10};
Output : 9

In the first input, if we delete 5 and 3 then the array becomes {9, 7, 10} and
the middle element will be 7.
In the second input, if we delete one element before 9 and two elements after 9 
(for example 2, 5, 8) then the array becomes {4, 3, 9, 7, 6, 10} and middle 
element will be 9 and it will be the optimum solution.

天真的方法:
天真的方法是检查所有可能的解决方案。可能有C(n,k)个可能的解。如果我们检查所有可能的解决方案以找到最佳解决方案,则将花费大量时间。
最佳方法:
删除k个元素后,该数组将缩小为n – k。因为我们可以从数组中删除任何k个数字,以找到最大可能的中间元素。如果我们注意到,删除k个元素后中间元素的索引将在(n + 1 – k)/ 2和(n + 1 – k)/ 2 + k的范围内。因此,为了找到最佳解决方案,只需将数组从索引(n + 1-k)/ 2迭代到索引(n + 1-k)/ 2 + k,然后选择此范围内的最大元素。
实现是在下面给出的。

C++
#include 
using namespace std;
 
// Function to calculate maximum possible middle
// value of the array after deleting exactly k
// elements
int maximum_middle_value(int n, int k, int arr[])
{
    // Initialize answer as -1
    int ans = -1;
 
    // Calculate range of elements that can give
    // maximum possible middle value of the array
    // since index of maximum possible middle
    // value after deleting exactly k elements from
    // array will lie in between low and high
    int low = (n + 1 - k) / 2;
 
    int high = (n + 1 - k) / 2 + k;
 
    // Find maximum element of the array in
    // range low and high
    for (int i = low; i <= high; i++) {
 
        // since indexing is 1 based so
        // check element at index i - 1
        ans = max(ans, arr[i - 1]);
    }
 
    // Return the maximum possible middle value
    //  of the array after deleting exactly k
    // elements from the array
    return ans;
}
 
// Driver Code
int main()
{
    int n = 5, k = 2;
    int arr[] = { 9, 5, 3, 7, 10 };
    cout << maximum_middle_value(n, k, arr) << endl;
 
    n = 9;
    k = 3;
    int arr1[] = { 2, 4, 3, 9, 5, 8, 7, 6, 10 };
    cout << maximum_middle_value(n, k, arr1) << endl;
 
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
 
class GFG
{
 
// Function to calculate maximum possible middle
// value of the array after deleting exactly k
// elements
static int maximum_middle_value(int n, int k, int arr[])
{
    // Initialize answer as -1
    int ans = -1;
 
    // Calculate range of elements that can give
    // maximum possible middle value of the array
    // since index of maximum possible middle
    // value after deleting exactly k elements from
    // array will lie in between low and high
    int low = (n + 1 - k) / 2;
 
    int high = (n + 1 - k) / 2 + k;
 
    // Find maximum element of the array in
    // range low and high
    for (int i = low; i <= high; i++)
    {
 
        // since indexing is 1 based so
        // check element at index i - 1
        ans = Math.max(ans, arr[i - 1]);
    }
 
    // Return the maximum possible middle value
    // of the array after deleting exactly k
    // elements from the array
    return ans;
}
 
// Driver Code
public static void main(String args[])
{
    int n = 5, k = 2;
    int arr[] = { 9, 5, 3, 7, 10 };
    System.out.println( maximum_middle_value(n, k, arr));
 
    n = 9;
    k = 3;
    int arr1[] = { 2, 4, 3, 9, 5, 8, 7, 6, 10 };
    System.out.println( maximum_middle_value(n, k, arr1));
}
}
 
// This code is contributed by Arnab Kundu


Python3
# Python3 implementation of the approach
 
# Function to calculate maximum possible
# middle value of the array after
# deleting exactly k elements
def maximum_middle_value(n, k, arr):
  
    # Initialize answer as -1
    ans = -1
 
    # Calculate range of elements that can give
    # maximum possible middle value of the array
    # since index of maximum possible middle
    # value after deleting exactly k elements
    # from array will lie in between low and high
    low = (n + 1 - k) // 2
 
    high = (n + 1 - k) // 2 + k
 
    # Find maximum element of the
    # array in range low and high
    for i in range(low, high+1): 
 
        # since indexing is 1 based so
        # check element at index i - 1
        ans = max(ans, arr[i - 1])
      
    # Return the maximum possible middle
    # value of the array after deleting
    # exactly k elements from the array
    return ans
  
# Driver Code
if __name__ == "__main__":
  
    n, k = 5, 2
    arr = [9, 5, 3, 7, 10]
    print(maximum_middle_value(n, k, arr))
 
    n, k = 9, 3
    arr1 = [2, 4, 3, 9, 5, 8, 7, 6, 10] 
    print(maximum_middle_value(n, k, arr1))
 
# This code is contributed by Rituraj Jain


C#
// C# implementation of the approach
using System;
 
class GFG
{
     
// Function to calculate maximum possible middle
// value of the array after deleting exactly k
// elements
static int maximum_middle_value(int n, int k, int []arr)
{
    // Initialize answer as -1
    int ans = -1;
 
    // Calculate range of elements that can give
    // maximum possible middle value of the array
    // since index of maximum possible middle
    // value after deleting exactly k elements from
    // array will lie in between low and high
    int low = (n + 1 - k) / 2;
 
    int high = (n + 1 - k) / 2 + k;
 
    // Find maximum element of the array in
    // range low and high
    for (int i = low; i <= high; i++)
    {
 
        // since indexing is 1 based so
        // check element at index i - 1
        ans = Math.Max(ans, arr[i - 1]);
    }
 
    // Return the maximum possible middle value
    // of the array after deleting exactly k
    // elements from the array
    return ans;
}
 
// Driver Code
static public void Main ()
{
         
    int n = 5, k = 2;
    int []arr = { 9, 5, 3, 7, 10 };
    Console.WriteLine( maximum_middle_value(n, k, arr));
 
    n = 9;
    k = 3;
    int []arr1 = { 2, 4, 3, 9, 5, 8, 7, 6, 10 };
    Console.WriteLine( maximum_middle_value(n, k, arr1));
}
}
 
// This code is contributed by ajit.


Javascript


输出:
7
9