Sherlock and MiniMax
Difficulty: Hard

Watson gives Sherlock an array of integers.  Given the endpoints of an integer range, for all $M$ in that inclusive range, determine the minimum( abs(arr[i]-M) for all $1 \le i \le |arr|$) ).  Once that has been determined for all integers in the range, return the $M$ which generated the maximum of those values.  If there are multiple $M$'s that result in that value, return the lowest one.  

For example, your array $arr = [3,5,7,9]$ and your range is from $p=6$ to $q=8$ inclusive.
```
M	|arr[1]-M|	|arr[2]-M|	|arr[3]-M|	|arr[4]-M|	Min
6	   3		   1		   1		   3		 1
7	   4		   2		   0		   2		 0
8	   5		   3		   1		   1		 1
```
We look at the `Min` column and see the maximum of those three values is $1$.  Two $M$'s result in that answer so we choose the lower value, $6$.

**Function Description**  

Complete the *sherlockAndMinimax* function in the editor below.  It should return an integer as described.  

sherlockAndMinimax has the following parameters:  
- *arr*: an array of integers  
- *p*: an integer that represents the lowest value of the range for $M$  
- *q*: an integer that represents the highest value of the range for $M$  

Input Format:
The first line contains an integer $n$, the number of elements in $arr$.  
The next line contains $n$ space-separated integers $arr[i]$.  
The third line contains two space-separated integers $p$ and $q$, the inclusive endpoints for the range of $M$.       


Output Format:
Print the value of $M$ on a line.

Sample Input:
	3
    5 8 14
    4 9


Sample Output:
	4


Python 3 starter code:
#
# Complete the 'sherlockAndMinimax' function below.
#
# The function is expected to return an INTEGER.
# The function accepts following parameters:
#  1. INTEGER_ARRAY arr
#  2. INTEGER p
#  3. INTEGER q
#

def sherlockAndMinimax(arr, p, q):
    # Write your code here




Reference solution (C++):
###C++  
```cpp
//C++ Code
Author Gerald
#ifdef ssu1
#define _GLIBCXX_DEBUG
#endif
#undef NDEBUG

#include <algorithm>
#include <functional>
#include <numeric>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <cassert>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <bitset>
#include <sstream>

using namespace std;

#define fore(i, l, r) for(int i = (l); i < (r); ++i)
#define forn(i, n) fore(i, 0, n)
#define fori(i, l, r) fore(i, l, (r) + 1)
#define sz(v) int((v).size())
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define mp make_pair
#define X first
#define Y second

#if ( _WIN32 || __WIN32__ )
   #define LLD "%I64d"
#else
   #define LLD "%lld"
#endif

typedef long long li;
typedef long double ld;
typedef pair<int, int> pt;

template<typename T> T abs(T a) { return a < 0 ? -a : a; }
template<typename T> T sqr(T a) { return a*a; }

const int INF = (int)1e9;
const ld EPS = 1e-9;
const ld PI = 3.1415926535897932384626433832795;

int readInt(int l, int r){
   int x;
   if(scanf("%d", &x) != 1){
       fprintf(stderr, "Expected int in range [%d, %d], but haven't found!", l, r);
       throw;
   }
   if(!(l <= x && x <= r)){
       fprintf(stderr, "Expected int in range [%d, %d], but found %d!", l, r, x);
       throw;
   }
   return x;
}

int f(const vector<int>& a, int m){
   int mn = (int)2e9;
   forn(i, sz(a)){
       mn = min(mn, abs(a[i] - m));
   }
   return mn;
}

int main(){
#ifdef ssu1
   assert(freopen("input.txt", "rt", stdin));
   //assert(freopen("output.txt", "wt", stdout));
#endif

   int n, p, q;
   n = readInt(1, 100);
   vector<int> a(n);
   forn(i, n)
       a[i] = readInt(1, 1000000000);
   p = readInt(1, 1000000000);
   q = readInt(1, 1000000000);
   assert(p <= q);

   vector<int> pat;
   pat.pb(p);
   pat.pb(q);
   forn(i, n){
       forn(j, n){
           int x = (a[i] + a[j]) / 2;
           fori(y, x - 1, x + 1){
               if(p <= y && y <= q)
                   pat.pb(y);
           }
       }
   }

   sort(all(pat));

   int curmx = -1, m = -1;
   forn(i, sz(pat)){
       if(f(a, pat[i]) > curmx){
           curmx = f(a, pat[i]);
           m = pat[i];
       }
   }
   printf("%d\n", m);
   return 0;
}
```