What is wrong with my code ?? I have been struggling with this for so long.
Can anyone help me debug this code ??
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int a, b;
cin >> a >> b;
vector<int> v;
for (int i = a; i <= b; i++) {
v.push_back(i);
}
sort(v.begin(), v.end());
int m = v.size() / 2;
int c = v[m];
cout << c << endl;
}
}
Can you help me what is wrong ???
I first push all values from a to b in a vector then I sort the vector to greedily select the median value.
Why is my logic wrong ?