# Maximum possible middle element of the array after deleting exactly k elements in C++

C++Server Side ProgrammingProgramming

#### C in Depth: The Complete C Programming Guide for Beginners

45 Lectures 4.5 hours

#### Practical C++: Learn C++ Basics Step by Step

Most Popular

50 Lectures 4.5 hours

#### Master C and Embedded C Programming- Learn as you go

66 Lectures 5.5 hours

In this tutorial, we will be discussing a program to find maximum possible middle element of the array after deleting exactly k elements

For this we will be provided with an array of size N and an integer K. Our task is to reduce K elements from the array such that the middle element of the resulting array is maximum.

## Example

Live Demo

#include <bits/stdc++.h>
using namespace std;
//calculating maximum value of middle element
int maximum_middle_value(int n, int k, int arr[]) {
int ans = -1;
int low = (n + 1 - k) / 2;
int high = (n + 1 - k) / 2 + k;
for (int i = low; i <= high; i++) {
ans = max(ans, arr[i - 1]);
}
return ans;
}
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;
}

## Output

7
9