Header Ad

HackerEarth Median Game problem solution

In this HackerEarth Median Game problem solution, You are given an array A of N integers. You perform this operation N - 2 times: For each contiguous subarray of odd size greater than 2, you find the median of each subarray(Say medians obtained in a move are m1,m2,m3,..,mk). In each move, you remove the first occurrence of value min(m1,m2,m3,...,mk) from the original array. After removing the element the array size reduces by 1 and no void spaces are left. For example, if we remove element 2 from the array {1,2,3,4}, the new array will be {1,3,4}.

Print a single integer denoting the sum of numbers that are left in the array after performing the operations. You need to do this for T test cases.


HackerEarth Median Game problem solution


HackerEarth Median Game problem solution.

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

#define IOS ios_base::sync_with_stdio(false); cin.tie(NULL);

int n, t;

int main() {
IOS;
cin>>t;
while(t--){
cin>>n;
int mn = INT_MAX, mx = -1;
for(int i = 0; i < n; i++){
int u;
cin>>u;
mx = max(mx, u);
mn = min(mn, u);
}
cout<<mx + mn<<"\n";
}
return 0;
}

Second solution

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5;
int main(){
ios::sync_with_stdio(0), cin.tie(0);
int t;
cin >> t;
while(t--){
int n;
cin >> n;
int mn = INT_MAX, mx = -mn;
while(n--){
int x;
cin >> x;
mn = min(mn, x);
mx = max(mx, x);
}
cout << mn + mx << '\n';
}
}


Post a Comment

0 Comments