Maximum possible middle element of the array after deleting exactly k elements
Last Updated :
09 Jun, 2022
Given an integer array of size n and a number k. If the indexing is 1 based then the middle element of the array is the element at index (n + 1) / 2, if n is odd otherwise n / 2. The task is to delete exactly k elements from the array in such a way that the middle element of the reduced array is as maximum as possible. Find the maximum possible middle element of the array after deleting exactly k elements.
Examples:
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.
Naive Approach :
The naive approach is to check all possible solutions. There could be C(n, k) possible solutions. If we check all possible solutions to find an optimal solution, it will consume a lot of time.
Optimal Approach :
After deleting k elements, the array will be reduced to size n - k. Since we can delete any k numbers from the array to find the maximum possible middle elements. If we note, the index of the middle element after deleting k elements will lie in the range ( n + 1 - k ) / 2 and ( n + 1 - k ) / 2 + k. So in order to find the optimal solution, simply iterate the array from the index ( n + 1 - k ) / 2 to index ( n + 1 - k ) / 2 + k and select the maximum element in this range.
The is the implementation is given below.
C++
#include <bits/stdc++.h>
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
<script>
// Function to calculate maximum possible middle
// value of the array after deleting exactly k
// elements
function maximum_middle_value(n, k, arr)
{
// Initialize answer as -1
let 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
let low = Math.floor((n + 1 - k) / 2);
let high = Math.floor(((n + 1 - k) / 2) + k);
// Find maximum element of the array in
// range low and high
for (let 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
let n = 5, k = 2;
let arr = [ 9, 5, 3, 7, 10 ];
document.write(maximum_middle_value(n, k, arr) + "<br>");
n = 9;
k = 3;
let arr1 = [ 2, 4, 3, 9, 5, 8, 7, 6, 10 ];
document.write(maximum_middle_value(n, k, arr1) + "<br>");
// This code is contributed by Mayank Tyagi
</script>
Time Complexity: O(high-low), where high and low are the terms calculated
Auxiliary Space: O(1), as no extra space is used
Similar Reads
Maximize minimum array element possible by exactly K decrements
Given an array arr[] consisting of N integers and an integer K, the task is to maximize the minimum element of the array after decrementing array elements exactly K number of times. Examples: Input: arr[] = {2, 4, 4}, K = 3 Output: 2Explanation:One of the possible way is: Decrement arr[2] by 1. The
7 min read
Maximize the sum of differences of consecutive elements after removing exactly K elements
Given a sorted array arr[] of length N and an integer K such that K < N, the task is to remove exactly K elements from the array such that the sum of the differences of the consecutive elements of the array is maximized.Examples: Input: arr[] = {1, 2, 3, 4}, K = 1 Output: 3 Let's consider all the
4 min read
Replace every array element with maximum of K next and K previous elements
Given an array arr, the task is to replace each array element by the maximum of K next and K previous elements. Example: Input: arr[] = {12, 5, 3, 9, 21, 36, 17}, K=2Output: 5 12 21 36 36 21 36 Input: arr[] = { 13, 21, 19}, K=1Output: 21, 19, 21 Naive Approach: Follow the below steps to solve this p
15 min read
Maximum frequency of any array element possible by at most K increments
Given an array arr[] of size N and an integer K, the task is to find the maximum possible frequency of any array element by at most K increments. Examples: Input: arr[] = {1, 4, 8, 13}, N = 4, K = 5 Output: 2 Explanation: Incrementing arr[0] twice modifies arr[] to {4, 4, 8, 13}. Maximum frequency =
15+ min read
Minimize the sum of differences of consecutive elements after removing exactly K elements
Given a sorted array of length 'N' and an integer 'K'(K<N), the task is to remove exactly 'K' elements from the array such that the sum of the difference of consecutive elements of the array is minimized.Examples: Input : arr[] = {1, 2, 3, 4}, k = 1 Output : 2 Let's consider all possible cases. a
12 min read
Maximum distinct elements after removing k elements
Given an array arr[] containing n elements. The task is to find the maximum count of distinct elements (non-repeating) after removing k elements from the array. Note: 1 <= k <= n.Examples: Input : arr[] = [5, 7, 5, 5, 1, 2, 2], k = 3Output : 4Explanation: Remove 2 occurrences of element 5 and
12 min read
Minimize maximum array element possible by at most K splits on the given array
Given an array arr[] consisting of N positive integers and a positive integer K, the task is to minimize the maximum element present in the array by splitting at most K array elements into two numbers equal to their value. Examples: Input: arr[] = {2, 4, 8, 2}, K = 4Output: 2Explanation:Following se
9 min read
Find K elements whose absolute difference with median of array is maximum
Given an array arr[] and an integer K, the task is to find the K elements of the array whose absolute difference with median of array is maximum. Note: If two elements have equal difference then the maximum element is taken into consideration. Examples: Input : arr[] = {1, 2, 3, 4, 5}, k = 3 Output
12 min read
Maximize unique elements in set after K deletions from each array
Given two 0-indexed integer arrays nums1[] and nums2[] of even length N. Remove K elements from nums1[] and K elements from nums2[]. After the removals, insert the remaining elements of nums1 and nums2 into a set S. The task is to return the maximum possible size of the set S. Examples: Input: nums1
10 min read
Minimize maximum array element by splitting array elements into powers of two at most K times
Given an array arr[] consisting of N positive integers and an integer K, the task is to minimize the maximum value of the array by splitting the array element into powers of 2 at most K times. Examples: Input: arr[] = {2, 4, 11, 2}, K = 2Output: 2Explanation:Below are the operations performed on arr
12 min read