Why is this code showing WA …it works fine on almost all testcases…Can someone Help out plz
#include<bits/stdc++.h>
using namespace std;
int main(){
int t;
cin >> t;
while(t--){
int n;
cin >> n;
map<int,int> p;
for(int i=0;i<n;i++){
int temp;
cin >> temp;
p[temp]++;
}
bool a = false;
for(auto it = p.begin();it != p.end();it++){
if(it->second > 2){
cout << "NO" << endl;
a = true;
break;
}
if(it-> second == 2 && (++it) == p.end() ){
cout << "NO" << endl;
a = true;
break;
}
}
if(a){
continue;
}else{
cout << "YES" << endl;
for(auto it = p.begin(); it != p.end() ;it++ ){
it->second -= 1;
cout << it->first << " ";
}
auto it = p.end();
it--;
for( ; ;it--){
if(it->second > 0){
cout << it->first << " ";
}
if(it == p.begin()){
break;
}
}
}
}
return 0;
}
This is my code, please provide testcase where my code goes wrong
https://www.codechef.com/viewsolution/34819485
I use same logic that explained above, but still got WA
You have not used endl after printing the sequence
I have two solution. One is working and other one is not . Can someone tell me the test case where it is getting rejected.
working solution = CodeChef: Practical coding for everyone
not working = CodeChef: Practical coding for everyone
Second soln is also getting tle . Somehow I solved this question in the contest. But it wasted lot of time. If someone can tell me why tle in the second solution, it would be appreciated.
Could you explain your logic on line 43?
Why are you comparing A[i] == a[i+1]?
https://www.codechef.com/viewsolution/34834875
C++ solution with detailed explanation in comments…
Please give it a read…
Since n can 2*10^3 at max, a simple hash algorithm will pass.
https://www.codechef.com/viewsolution/34794934
Bro, as you are using insert and erase function inside for loop that’s why Time complexity of your program is O(N^2) that’swhy it is showing TLE. Try to convert it into O(N*Logn).
1 Like
Even i am getting AC in 2nd subtask and WA in the first one
pls find the error
#include <bits/stdc++.h>
#include
using namespace std;
int main() {
int t;
cin>>t;
while(t–){
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
sort(a,a+n);
int c=0;
for(int i=0;i<n-1;i++){
if(a[i]==a[i+1]){
c++;
break;
}
}
if(c==0){
cout<<“YES”<<endl;
for(int i=0;i<n;i++){
cout<<a[i]<<" “;
}
cout<<endl;
}
else{
c=0;
if(a[n-2]==a[n-1])
cout<<“NO”<<endl;
else{
int b[n],j=0;
for(int i=0;i<n-1;i++){
if(a[i]==a[i+1]){
b[j]=i+1;
j++;
c++;
}
else c=0;
if(c>=2)
break;
}
int x=j-1;
if(c>=2)
cout<<“NO”<<endl;
else{
cout<<“YES”<<endl;
j=0;
for(int i=0;i<n;i++){
if(i!=b[j])
cout<<a[i]<<” “;
else j++;
}
for(j=x;j>=0;j–){
cout<<a[b[j]]<<” ";
}
cout<<endl;
}
}
}
}
return 0;
}
wrango
June 28, 2020, 8:43am
36
Do not print each element of the permuted sequence individually.
Try to store the elements of the permuted sequence into string builder object and then print it.
https://www.codechef.com/viewsolution/34838305
2 Likes
bada55
June 28, 2020, 12:25pm
38
https://www.codechef.com/viewsolution/34843553
Could anyone tell me why its giving worng answer
The logic is same as the editorial. i cannot find any mistake in here
//can anyone tell me the mistake in this code.it works fine for given test case but getting WA
#include
#include
#include
using namespace std;
int main(){
int t;
cin>>t;
while(t--){
int n;
cin>>n;
int arr[n];
for(int i =0 ; i<n ; i++)
cin>>arr[i];
int flag = 1;
int prev = INT_MAX;
int count = 1;
sort(arr , arr + n);
for(int i = 0 ; i<n ; i++){
if(count > 2){
cout<<"NO"<<endl;
flag = 0;
break;
}
else{
if(prev == arr[i]){
count++;
}
else{
count = 1;
}
}
prev = arr[i];
}
if(arr[n-1] == arr[n-2] && flag){
cout<<"NO"<<endl;
flag = 0;
break;
}
if(flag){
int p = INT_MAX;
cout<<"YES"<<endl;
for(int i = 0 ; i<n; i++){
if(p != arr[i]){
p = arr[i];
cout<<arr[i]<<" ";
arr[i] = -1;
}
}
sort(arr,arr+n , greater<int>());
for(int i = 0 ; i<n ; i++){
if(arr[i] != -1)
cout<<arr[i]<<" ";
}
cout<<endl;
}
}
}
1 Like
Hello. Can anyone please tell me what’s wrong in my solution? It passes all the given test cases but gives WA for both subtasks.
https://www.codechef.com/viewsolution/34848832
Nice approach but it can be way simpler. cheers !!
it will check whether three elements are equal or not