1610B - Kalindrome Array - CodeForces Solution


greedy two pointers *1100

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
using namespace std;
bool plaindrome(vector<int>&v,int n){
int a = n/2;
for(int i =0;i<a;i++){
  if(v[i]!=v[n-i-1])
  return false;
} 
return true;
}





int main(){
    int t;
    cin>>t;
    while(t--){
      int n;
      cin>>n;
      vector<int>v;
      for(int i =0;i<n;i++){
        int x;
        cin>>x;
        v.push_back(x);
      }
      if(n==2){
        cout<<"YES"<<endl;
        continue;
      }
   unordered_map<int,int>mp;
   for(int i =0;i<n;i++){
    mp[v[i]]++;
   }

    vector<int>a,b,c,d;
       if(v[0]!=v[n-1]){
        if((mp[v[0]]==1)&&(mp[v[n-1]]==1)){
          cout<<"NO"<<endl;
          continue;
        }
        if((mp[v[0]]>1) || (mp[v[n-1]]>1)){
          for(int i =0;i<n;i++){
            if(v[i]!=v[0]){
              a.push_back(v[i]);
            }
            if(v[i]!=v[n-1]){
              b.push_back(v[i]);
            }
          } 
          if(plaindrome(a,a.size())||plaindrome(b,b.size())) cout<<"YES"<<endl;
          else cout<<"NO"<<endl;
          continue;
        }




       }  

       else { 
        int flag =0;
        int p =0,q=0;
        int i =0 , j = n-1;
        while(i<j){
          if(v[i]==v[j]){
            i++;
            j--;
          } 
          else{
             p= v[i];
             q = v[j]; 
             flag = 1;
             break;
          }


        } 
        if(flag==0){
          cout<<"YES"<<endl;
          continue;
        }

         for(int i =0;i<n;i++){
          if(v[i]!=p){
            c.push_back(v[i]);

          }
          if(v[i]!=q) d.push_back(v[i]);

         } 
         if(plaindrome(c,c.size())||plaindrome(d,d.size())) cout<<"YES"<<endl;
          else cout<<"NO"<<endl;
          continue;


       }
       





    }

}


Comments

Submit
0 Comments
More Questions

678A - Johny Likes Numbers
1699C - The Third Problem
1697D - Guess The String
754B - Ilya and tic-tac-toe game
760A - Petr and a calendar
1573A - Countdown
166A - Rank List
1631B - Fun with Even Subarrays
727A - Transformation from A to B
822B - Crossword solving
1623A - Robot Cleaner
884B - Japanese Crosswords Strike Back
862B - Mahmoud and Ehab and the bipartiteness
429A - Xor-tree
1675C - Detective Task
950A - Left-handers Right-handers and Ambidexters
672B - Different is Good
1C - Ancient Berland Circus
721A - One-dimensional Japanese Crossword
1715B - Beautiful Array
60B - Serial Time
453A - Little Pony and Expected Maximum
1715A - Crossmarket
1715C - Monoblock
1512C - A-B Palindrome
1679B - Stone Age Problem
402A - Nuts
792A - New Bus Route
221A - Little Elephant and Function
492C - Vanya and Exams