Deleting Numbers

Zenyk recently got an array with his 

n school grades a1,a2,...,an. He isn't very happy with them and knows that his parents also will not be happy with his grades. But he also knows that his parents evaluate his performance in a very strange way. They care only about the middle element in the array of grades Zenyk got. More formally if Zenyk's array is b and it has m elements, b(m+1)/2 is Zenyk's performance. Zenyk doesn't want to disappoint his parents, so he wants to erase exactly k(k<n) grades from his array a in order to maximize his score.

Example:

Input: n = 5, k = 2, arr[n] = {47, 4, 7, 44, 77}
Output: 44

Approach

C++

#include <bits/stdc++.h>
using namespace std;

int main()
{

    int n = 5k = 2;

    int arr[n] = {47474477};

    cout << *max_element(arr + (n - k - 1) / 2,
                         arr + (n - (n - k) / 2));
    return 0;
}


No comments:

Post a Comment