krushn
1
So i am getting a sigcont error on the code that i have written and i think that is the reason for my solution not being accepted.
here is the submission link: CodeChef
Here is the code:
#include
using namespace std;
int main() {
long long T;
cin>>T;
while(T–){
long long x;
cin>>x;
if(x<=4){
cout<<-1;
}else{
if(x%2 == 0){
for(long i =1;i<x/2+1;i++){
cout<<i<<" “<<x/2+i<<” “;
}
}else{
for(long i = 1;i<x/2+1;i++){
cout<<i<<” “<<x/2+1+i<<” “;
}
cout<<x/2+1;
}
}
cout<<”\n";
}
}
could someone help me understand whats the problem here??
This is because you’re not considering the test case N = 4
. When N = 4
, your answer should be 3 1 4 2
not -1
.
Here’s my solution:
#include <iostream>
#include <vector>
using namespace std;
int main() {
// your code goes here
int t;
cin >> t;
while (t--) {
int N;
cin >> N;
if (N < 4) {
cout << -1 << "\n";
}
else if (N == 4) {
cout << 3 <<" "<< 1 <<" "<< 4 <<" "<< 2 << "\n";
}
else {
int middle;
if (N%2 == 0) {
middle = N / 2;
}
else {
middle = (N + 1) / 2;
}
int a = 1;
int b = middle + 1;
vector<int> v1;
for (int i = 0; i < middle; i++) {
v1.push_back(a);
if (v1.size() != N) {
v1.push_back(b);
}
a += 1;
b += 1;
}
for (auto itr = v1.begin(); itr != v1.end(); ++itr)
cout << *itr << " ";
}
}
return 0;
}
Before this my solution I was also doing the same mistake, we should think about edge test cases more carefully. All the best