[Mảng 1 Chiều Cơ Bản]. Bài 17. Mảng đối xứng

View as PDF

Submit solution

Points: 1.00 (partial)
Time limit: 1.0s
Memory limit: 256M
Input: stdin
Output: stdout

Problem source:
28Tech
Problem type
Allowed languages
C, C#, C++, Java, Kotlin, Pascal, PyPy, Python, Scratch

Cho mảng số nguyên A[] gồm N phần tử, kiểm tra xem mảng có đối xứng hay không?


Đầu vào
  • Dòng đầu tiên là số nguyên dương N

  • Dòng thứ 2 gồm N số nguyên viết cách nhau một vài khoảng trắng


Giới hạn
  • 1<=N<=10^6

  • 0<=A[i]<=10^6


Đầu ra
  • In ra YES hoặc NO tương ứng với mảng đối xứng hoặc không.

Ví dụ :

Input 01
5
1 2 3 2 1
Output 01
YES

Comments

Please read the guidelines before commenting.



  • 0
    Lam_Thang2014  commented on July 22, 2026, 1:40 p.m.

    include <iostream>

    include <vector>

    using namespace std;

    int main() {

    ios::syncwithstdio(false);

    cin.tie(nullptr);

    int num; cin >> num;

    vector<int> vec(num);

    for (int &ele : vec) {

    cin >> ele;

    }

    for (int idx = 0; idx <= num / 2; idx++) {

    if (vec[idx] != vec[num - 1 - idx]) {

    cout << "NO";

    return 0; }

    }

    cout << "YES";

    }


  • 0
    Hung123456  commented on July 7, 2026, 2:45 p.m.

    full AC

    include <bits/stdc++.h>

    using namespace std;

    int main() { iosbase::syncwith_stdio(false); cin.tie(NULL); int n;cin>>n; int a[n]; map<int,int>mp; for (int i=0;i<n;i++){ cin>>a[i]; mp[a[i]]++; } int sum=0; if (n%2==0){ for (int i=0;i<n;i++){ if (mp[a[i]]%2==1){ sum=1; break; } } if (sum==0){ cout<<"YES"; } else cout<<"NO"; } else { for (int i=0;i<n;i++){ if (mp[a[i]%2==1]){ sum++; } } if (sum==1){ cout<<"YES"; } else cout<<"NO"; } return 0; }


  • 1
    minhduc2803  commented on June 20, 2026, 10:32 a.m.

    ý tưởng cho ae nào bí nhé : FULL AC:

    đầu tiên ae tạo 1 biến cờ hiệu đặt giá trị là 1 hoặc true cũng được, sau đó ae tạo 1 vòng lặp chạy từ 0 tới n/2 nhé, vì chỉ cần tới nửa mảng là đủ để kiếm tra rồi, sau đó ae kiểm tra xem phần tử i có khác với a[n-1-i] ko, n-1-i là phần tử đối diện của phần tử i nhé VD: i = 0 thì n - 1 - i = 5 - 1 - 0 là bằng 4 thì nó đối diện với i, nếu khác thì ae đặt biến cờ hiệu thành 0 hoặc false nhé, rồi ae break câu lệnh luôn vì đã tìm ra được cặp khác nhau rồi thì mảng ko có đối xứng nữa cho nên lặp thêm cũng chỉ tốn thời gian, rồi ae ở ngoài vòng lặp kiểm tra xem biến cờ hiệu có bằng false ko nếu bằng thì cout ra no , còn ko thì cout ra yes.


  • 0
    Kelvin2206  commented on May 31, 2026, 3:09 a.m.

    include <bits/stdc++.h>

    using namespace std;
    
    typedef long long ll;
    typedef unsigned long long ull;
    typedef long double ld;
    typedef string str;
    typedef vector<ll> vi;
    typedef pair<ll,ll> pi;
    
    #define pb push_back
    #define all(x) x.begin(), x.end()
    #define el '\n'
    
    const ll INF=1e18, MOD=1e9+7, MXA=1e5+5;
    
    ll n;
    vi v, v0;
    
    void Solve() {
        cin >> n; v.resize(n); for(auto &x:v) cin >> x;
        for(ll i=n-1; i>=0; --i) v0.pb(v[i]);
    
        for(ll i=0; i&lt;n; ++i) {
            if(v[i]==v0[i]) {
                if(i==n-1) cout << "YES";
            }
            else {
                cout << "NO"; break;
            }
        }
    }
    
    int main() {
        //freopen("", "r", stdin); freopen("", "w", stdout);
        ios::sync_with_stdio(0); cin.tie(nullptr);
    
        ll tests=1; //cin >> tests;
        while(tests--) Solve();
    }
    

  • -3
    trinhdinhhoan2k12  commented on May 3, 2026, 1:39 p.m.

    67


  • -5
    trinhdinhhoan2k12  commented on May 3, 2026, 1:30 p.m.

    This comment is hidden due to too much negative feedback. Show it anyway.


  • -1
    kietne  commented on Dec. 13, 2025, 2:15 a.m.

    HAI CON TRỎ LÀ KHOẺ NHÉ AE

    include <bits/stdc++.h>

    using namespace std; using ll = long long;

    int main() { ios::syncwithstdio(false); cin.tie(nullptr); // viết code ở đây int n ; cin >> n; int a[n]; for (auto &i : a) cin >> i; int i=0,j=n-1; bool done=false; while(i<=j){ if(a[i]!=a[j]) done=true; i++; j--; } if(done) cout << "NO"; else{ cout << "YES"; } return 0; }


  • 0
    naipret  commented on Sept. 18, 2025, 7:28 a.m.
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    int main() {
      ios::sync_with_stdio(false);
      cin.tie(nullptr);
    
      int num;
      cin >> num;
    
      vector<int> vec(num);
      for (int &ele : vec) {
        cin >> ele;
      }
    
      for (int idx = 0; idx <= num / 2; idx++) {
        if (vec[idx] != vec[num - 1 - idx]) {
          cout << "NO";
          return 0;
        }
      }
      cout << "YES";
    }
    

  • 0
    id_07  commented on Sept. 6, 2025, 3:03 a.m.
    #include <bits/stdc++.h>
    using namespace std;
    int main() {
        int n;cin>>n;
        int a[n];
        for(int i=0;i&lt;n;i++) cin>>a[i];
    bool f=true;
    for(int i=0;i&lt;n/2;i++) {
        if(a[i]!=a[n-i-1]) f=false;
    }
    if(!f) cout << "NO";
    else cout <<"YES";
    }
    

  • 1
    hngoc  commented on July 4, 2025, 2:26 p.m.
    import math
    n=input()
    a=input().split()
    b=a.copy()
    b.reverse()
    print('YES' if a==b else 'NO')
    

  • 0
    bengokyeuanh99  commented on May 30, 2025, 6:59 p.m.

    include <cstdio>

    constexpr int MAXN = 1000000; int arr[MAXN];

    int main() { int n; if (scanf("%d", &n) != 1) return 1;

    for (int i = 0; i < n; ++i)
        if (scanf("%d", &arr[i]) != 1) return 1;
    
    for (int i = 0; i < n / 2; ++i)
        if (arr[i] != arr[n - 1 - i]) {
            puts("NO");
            return 0;
        }
    
    puts("YES");
    return 0;
    

    }


  • 5
    VDev  commented on Jan. 18, 2025, 1:08 p.m.

    FULL AC

    #include <bits/stdc++.h>
    #include <iomanip>
    #include <cmath>
    #include <climits>
    #define ll long long
    using namespace std;
    ll a[10000011];
    ll cnt = 0, s = 0;
    
    int main(){
        ll n;
        cin >> n;
        bool check = true;
        for(ll i = 0; i < n; i++){
            cin >> a[i];
        }
        for(ll i = 0; i < n / 2; i++){
                if(a[i] != a[n - i - 1]){
                    check = false;
                    break;
                }
        }
        if(check){
                cout << "YES";
        }else{
             cout << "NO";
        }
        return 0;
    }
    

  • -1
    tandatha140904  commented on Dec. 25, 2024, 4:50 p.m.

    include <bits/stdc++.h>

    using namespace std; int doixung(int a[], int n) { int mid = n / 2, i = 1, j = n; while (i <= mid && j > mid) { if (a[i] == a[j]) { i++; j--; } else return 0; } return 1; } int main() { int n; cin >> n; int a[n]; for (int i = 1; i <= n; i++) { cin >> a[i]; } if (doixung(a, n)) { cout << "YES"; } else cout << "NO"; return 0; }


  • -1
    dangsp07  commented on Dec. 12, 2024, 11:41 a.m.

    include <iostream>

    include <vector>

    using namespace std;

    int main() { int n; cin >> n; vector<int> a(n); for (int &x : a) cin >> x; for (int i = 0; i < n / 2; i++) { if (a[i] != a[n - i - 1]) { cout << "NO\n"; return 0; } }

    cout << "YES\n";
    return 0;
    

    }


  • 0
    janekdao  commented on Nov. 30, 2024, 8:27 a.m.

    hay


  • -2
    ichingu  commented on Nov. 8, 2024, 12:50 p.m.


  • -5
    Nakbosstv  commented on Sept. 16, 2024, 3:00 p.m.

    This comment is hidden due to too much negative feedback. Show it anyway.


  • -7
    NTH11112222  commented on June 12, 2024, 2:55 p.m.

    This comment is hidden due to too much negative feedback. Show it anyway.


  • -8
    Itachi  commented on May 4, 2024, 3:10 p.m.

    This comment is hidden due to too much negative feedback. Show it anyway.


  • -18
    2vhoc7  commented on April 1, 2024, 4:17 p.m.

    This comment is hidden due to too much negative feedback. Show it anyway.