Simple Statistics. Link : https://www.codechef.com/problems/SIMPSTAT

My code works on my own compiler but getting WA on CodeChef. Any help is appreciated!

#include
#include
#include
#include

int main() {
int tc = 0;
std::cin >> tc;
while(tc–) {
int N = 0;
std::cin >> N;

int K = 0;
std::cin >> K;

std::deque<int> a(N);

for (int i = 0; i < N; ++i) {
  std::cin >> a[i];
}

std::sort(a.begin(), a.end());


for (int i = 0; i < K; ++i) {
  a.pop_front();
  a.pop_back();
}

int sum = 0;
for (int i = 0; i < a.size(); ++i) {
  sum += a[i];
}

int avg = sum / a.size();
std::cout << std::setprecision(6) << avg << std::endl;

}
}

I have corrected your code : link : CodeChef: Practical coding for everyone
let me point out your mistakes
you have taken variable avg as integer so fractional part will be omitted and it would give a WA verdict
your output statement has some flaws (if ans is 5.4 your output should be 5.400000 but your code gave output 5.4 which is wrong )
change your output statement to cout<<fixed<<setprecision(6)<<avg<<endl;

Secondly ,I would like to give you some advice , deque was not needed.It could be done simply using arrays so try to keep the code simple as possible.

1 Like