Anyone help me to find problem in my solution because it cannot pass hissen test case.
#include <bits/stdc++.h>
using namespace std;
int main() {
// your code goes here
int t;
cin>>t;
while(t–){
long long a,b;
cin>>a>>b;
if(a%2==0&&b%2==0){
cout<<string(a/2,‘a’)+string(b,‘b’)+string(a/2,‘a’)<<endl;
cout<<string(b/2,‘b’)+string(a,‘a’)+string(b/2,‘b’)<<endl;
}
else if(b%2==0){
cout<<string(a/2,‘a’)+string(b/2,‘b’)+‘a’+string(b/2,‘b’)+string(a/2,‘a’)<<endl;
cout<<string(b/2,‘b’)+string(a,‘a’)+string(b/2,‘b’)<<endl;
}
else if(a%2==0){
cout<<string(b/2,‘b’)+string(a/2,‘a’)+‘b’+string(a/2,‘a’)+string(b/2,‘b’)<<endl;
cout<<string(a/2,‘a’)+string(b,‘b’)+string(a/2,‘a’)<<endl;
}
else{
cout<<-1<<endl;
}
}
return 0;
}
Hi there.
For someone to be able to help you, you will need to provide a link to the problem you’re trying to solve. Also, it helps a lot if you format your code properly, like this:
#include <bits/stdc++.h>
using namespace std;
int main() {
// your code goes here
int t;
cin >> t;
while (t--) {
long long a, b;
cin >> a >> b;
if (a % 2 == 0 && b % 2 == 0) {
cout << string(a / 2,'a') + string(b,'b') + string(a / 2,'a') << endl;
cout << string(b / 2,'b') + string(a,'a') + string(b / 2,'b') << endl;
} else if (b % 2 == 0) {
cout << string(a / 2,'a') + string(b / 2,'b') +'a'+ string(b / 2,'b') +
string(a / 2,'a')
<< endl;
cout << string(b / 2,'b') + string(a,'a') + string(b / 2,'b') << endl;
} else if (a % 2 == 0) {
cout << string(b / 2,'b') + string(a / 2,'a') +'b'+ string(a / 2,'a') +
string(b / 2,'b')
<< endl;
cout << string(a / 2,'a') + string(b,'b') + string(a / 2,'a') << endl;
} else {
cout << -1 << endl;
}
}
return 0;
}
On the presumption that this is PALINPAIN , your code fails on this test case, for which the expected answer is ‘-1’:
1
2 1
[Edit: linked to wrong problem, sorry]
Try this case:
7
1 1
1 2
2 1
1 3
3 1
1 4
4 1
should all be -1, yours is
-1
bab
bab
aba
aba
-1
-1
bbabb
bbabb
aabaa
aabaa
I did this but this gave me wrong answer???
#include
#include
#include
#include
using namespace std;
int main(){
int t; cin>>t;
while(t–){
int x,y; cin>>x>>y;
char a=‘a’,b=‘b’;
if(x==1 || y==1 || (x%2==1 && y%2==1))
cout<<-1<<endl;
else{
cout<<string((x/2),a)+string(y,b)+string((x/2),a)<<endl;
cout<<string((y/2),b)+string(x,a)+string((y/2),b)<<endl;
}
}
return 0;
}
demo_neel:
… snip…
Please format your code properly. using ‘pre-formatted text’, otherwise the editor mangles it.
Try this input, which should show you where you’re going wrong:
2
5 4
4 5
Check the following test case:
1 2
Your code output is:
bab
bab
while the correct answer is:
-1
as both palindrome strings are equal, and it is impossible to generate two different palindrome strings in this case. The issue with your solution is that when either X or Y is even and the other number is equal to 1 , it is impossible to generate two different palindrome strings in this corner case.
Check the palindrome_pair class in the following solution.
Accepted