[Mảng 1 Chiều Cơ Bản]. Bài 58. Cặp đôi xa cách

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++, Java, Kotlin, Pascal, PyPy, Python, Scratch

Cho mảng A[] gồm N phần tử bạn hãy tìm 2 số A[i]A[j] trong mảng với A[i] = A[j]i < j sao cho j - i đạt giá trị lớn nhất.

Ví dụ : A = [1, 2, 3, 1, 1, 5] thì cặp i = 0 và j = 4 là kết quả tốt nhất.

Bạn hãy tìm giá trị lớn nhất của j - i, nếu trong mảng không tồn tại cặp A[i]A[j] (i < j) bằng nhau thì bạn hãy in ra 28tech.


Đầu vào

Dòng 1 là N : số phần tử trong mảng

Dòng 2 là N số viết cách nhau 1 dấu cách


Giới hạn

1≤N≤10^3

0≤A[i] ≤10^6


Đầu ra

In ra đáp án của bài toán


Ví dụ :

Input 01
6
1 2 3 1 1 5
Output 01
4
Input 02
5
1 2 3 4 5
Output 02
28tech

Comments

Please read the guidelines before commenting.



  • 0
    kietne  commented on Dec. 29, 2025, 2:13 p.m.

    include <bits/stdc++.h>

    using namespace std; using ll = long long; using ull = unsigned long long;

    int main() { ios::syncwithstdio(false); cin.tie(nullptr); int n;cin >> n; int a[n]; for(int &i : a) cin >> i; int max=0; for(int i = 0; i < n; i++){ for(int j = i+1; j < n;j++){ if(a[i]==a[j]){ if(max<(j-i)) max=(j-i); } } } if(max==0)cout << "28tech"; else cout << max_; return 0; }


  • -3
    anhthuy_  commented on Nov. 26, 2025, 9:02 a.m.

    full ac cho sigma

    #include <bits/stdc++.h>
    #define iosf ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
    #define fi first
    #define se second
    using namespace std;
    template &lt;typename T,typename P> using pa = pair<T,P>;
    
    int main(){
        iosf
    
        int n;
    
        cin >> n;
    
        map<int,pa<int,int>> mp;
        int index = 1;
    
        while(n--){
            int num; cin >> num;
    
            if (mp.count(num) != 1){
                mp[num] = {index,index};
            } else {
                mp[num].fi = min(mp[num].fi,index);
                mp[num].se = max(mp[num].se,index);
            }
    
            index++;
        }
    
        int MAX = -1;
        bool none = true;
    
        for(auto [num,index] : mp){
            if (index.fi == index.se) continue;
    
            none = false;
    
            int farthest = index.second;
            int nearest = index.first;
    
            MAX = max(MAX,farthest-nearest);
        }
    
        if (none) {
            cout << "28tech";
        } else {
            cout << MAX;
        }
    
        return 0;
    }