Header Ad

HackerEarth Deleting Numbers problem solution

In this HackerEarth Deleting Numbers problem solution, 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.


HackerEarth Deleting Numbers problem solution


HackerEarth Deleting Numbers problem solution.

#include <bits/stdc++.h>
using namespace std;
int n , k;
int a[100005];
int main()
{
cin >> n >> k;
for(int i = 1; i <= n; i++)
cin >> a[i];
int l = (n + 1) / 2 , r = (n + 1) / 2;
bool odd = n % 2;
for(int i = 1; i <= k; i++)
{
int par = (i % 2) ^ odd;
if(par)
r++;
else
l--;
}
int maxNumber = 0;
for(int i = l; i <= r; i++)
maxNumber = max(maxNumber , a[i]);
cout << maxNumber;
}


Second solution

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 14;
int n, k;
int main(){
ios::sync_with_stdio(0), cin.tie(0);
cin >> n >> k;
int ans = 0;
for(int i = 0; i < n; i++){
int x;
cin >> x;
if(i >= (n - k - 1) / 2 && i <= n - (n - k) / 2 - 1)
ans = max(ans, x);
}
cout << ans << '\n';
}


Post a Comment

0 Comments