Consider this input
1
5
6 5 1 4 10
Your Output
2
3 3
4 3
Sequence after moves: [6, 5, 0, 4, 10]
which is not \text{tasty}. It was also mentioned i\ne j but your first move doesn’t satisfy it.
Consider this input
1
5
6 5 1 4 10
Your Output
2
3 3
4 3
Sequence after moves: [6, 5, 0, 4, 10]
which is not \text{tasty}. It was also mentioned i\ne j but your first move doesn’t satisfy it.
Can anyone please tell me the test case for which this solution is failing? Also what will be the output if an already tasty sequence is given.
The link of my solution is here.
Thank u
It should be 0.
Try this-
1
9
1 0 1 1 0 1 0 1 1
Also i \neq j for a valid operation.
Thanks bro… I got my mistake
can you tell me this will bee a tasty sequence?
What happens if the seq is instead all odd?
That’s not a special case at all. Indeed, that’s the most trivial thing you can answer. The minimum moves required will be \lfloor{\cfrac{N}{2}}\rfloor where N is the length of the sequence. And the output will be something like this:
N/2
1 2
1 4
1 6
...
1 N // print last line only if N is even
can anyone tell me why my code is giving wrong ans?
#include<iostream>
#include<vector>
using namespace std;
#define fio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
int main()
{fio;
int t;
cin>>t;
int n;
while(t--)
{
cin>>n;
int oddexist=0,odd=0,even=0,m=0,oddindex=0,evenindex=0;
vector<int> v(n);
vector<pair<int,int> >ans;
for(int i=0;i<n;i++)
{
cin>>v[i];
if(!(i&1))
{
if(v[i]&1)
{
oddindex=i;
even++;
oddexist++;
}
else
{
odd++;
evenindex=i;
}
}
else
{
if(!(v[i]&1))
{
even++;
evenindex=i;
}
else
{
odd++;
oddexist++;
oddindex=i;
}
}
}
//cout<<oddexist<<" "<<even<<" "<<odd<<" ";
if(!oddexist)m=0;
else
{
if(odd>even)
{
//start with even
for(int i=0;i<n;i++)
{
if(!(i&1))
{
if(v[i]&1)
{
v[i]=v[i]^v[evenindex];
m++;
ans.push_back({i,evenindex});
evenindex=i;
}
else
{
evenindex=i;
}
}
else
{
if(!(v[i]&1))
{
//chnage
v[i]=v[i]^v[oddindex];
m++;
ans.push_back({i,oddindex});
oddindex=i;
}
else
{
oddindex=i;
}
}
}
}
else
{
//start with the odd
for(int i=0;i<n;i++)
{
if(!(i&1))
{
if(v[i]&1)
{
oddindex=i;
}
else
{
//change
v[i]=v[i]^v[oddindex];
m++;
ans.push_back({i,oddindex});
oddindex=i;
}
}
else
{
if(v[i]&1)
{
//change
v[i]=v[i]^v[oddindex];
m++;
ans.push_back({i,oddindex});
evenindex=i;
}
else
{
evenindex=i;
}
}
}
}
}
if(m)
{
cout<<m<<"\n";
for(auto x: ans)
{
cout<<x.first+1<<" "<<x.second+1<<"\n";
}
}
else
{
cout<<-1<<"\n";
}
}
return 0;
}
can you tell me why my code is giving wrong ans?
#include<iostream>
#include<vector>
using namespace std;
#define fio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
int main()
{fio;
int t;
cin>>t;
int n;
while(t--)
{
cin>>n;
int oddexist=0,odd=0,even=0,m=0,oddindex=0,evenindex=0;
vector<int> v(n);
vector<pair<int,int> >ans;
for(int i=0;i<n;i++)
{
cin>>v[i];
if(!(i&1))
{
if(v[i]&1)
{
oddindex=i;
even++;
oddexist++;
}
else
{
odd++;
evenindex=i;
}
}
else
{
if(!(v[i]&1))
{
even++;
evenindex=i;
}
else
{
odd++;
oddexist++;
oddindex=i;
}
}
}
//cout<<oddexist<<" "<<even<<" "<<odd<<" ";
if(!oddexist)m=0;
else
{
if(odd>even)
{
//start with even
for(int i=0;i<n;i++)
{
if(!(i&1))
{
if(v[i]&1)
{
v[i]=v[i]^v[evenindex];
m++;
ans.push_back({i,evenindex});
evenindex=i;
}
else
{
evenindex=i;
}
}
else
{
if(!(v[i]&1))
{
//chnage
v[i]=v[i]^v[oddindex];
m++;
ans.push_back({i,oddindex});
oddindex=i;
}
else
{
oddindex=i;
}
}
}
}
else
{
//start with the odd
for(int i=0;i<n;i++)
{
if(!(i&1))
{
if(v[i]&1)
{
oddindex=i;
}
else
{
//change
v[i]=v[i]^v[oddindex];
m++;
ans.push_back({i,oddindex});
oddindex=i;
}
}
else
{
if(v[i]&1)
{
//change
v[i]=v[i]^v[oddindex];
m++;
ans.push_back({i,oddindex});
evenindex=i;
}
else
{
evenindex=i;
}
}
}
}
}
if(m)
{
cout<<m<<"\n";
for(auto x: ans)
{
cout<<x.first+1<<" "<<x.second+1<<"\n";
}
}
else
{
cout<<-1<<"\n";
}
}
return 0;
}
Consider this input:
1
5
4 1 2 4 5
Your Output:
2
4 2
5 3
Sequence at the end: 4 1 2 5 7
which is not \text{tasty}.