I know that blaming the compiler should be the last resort. However, I know the site has had some overhauls. If not, it is a bug that I do not understand.
My code is as follows
#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
using namespace std;
int main() {
int t;
cin >> t;
while(t--){
int n;
int a[n];
int b[n];
cin>> n;
for (int i=0; i< n; i++){
cin >> a[i];
}
for (int i=0; i<n; i++){
cin >> b[i];
}
for (int i=0; i< n; i++){
cout << a[i] << " ";
}cout << endl;
for (int i=0; i<n; i++){
cout << b[i] << " ";
}cout << endl;
sort(a, a+n);
sort(b, b+n);
int median = a[n-1] + b[n-1];
for (int i=0; i< n; i++){
cout << a[i] << " ";
}cout << endl;
for (int i=0; i<n; i++){
cout << b[i] << " ";
}cout << endl;
for (int i=n/2; i<n; i++){
int temp = a[i] + b[n-1 - i + n/2];
if (temp < median){
cout << i << " " << a[i] << " " << b[n-1 - i + n/2] << endl;
median = temp;
}
}
cout << median <<endl;
}
// your code goes here
return 0;
}
I would expect this to output the unsorted array, then the sorted array. However, every time I check the array two numbers have swapped.
I figured that this may be an overflow error based on the arrays being next to each other. However, as far as I can see I do not access any point that should be outside either array.
The code works as intended for the first two test cases with input
3
1
10
25
3
1 6 6
2 2 7
5
10 4 93 5 16
4 34 62 6 26
and output
10
25
10
25
1 6 6
2 2 7
1 6 6
2 2 7
26 4 93 5 16
4 34 62 6 26
62 5 16 26 93
4 4 6 34 62
As can be seen, in the last test case numbers are swapping. Since I have removed most of the extra stuff from the code, I cannot see where the error is being introduced.
Thanks in advance.