Untitled

mail@pastecode.io avatar
unknown
plain_text
5 months ago
982 B
6
Indexable
#include <bits/stdc++.h>
using namespace std;

int dem(int arr[], int n) {
    sort(arr, arr + n);
    
    int count = 0;
    if (n < 3) return 0;

    for (int b = 0; b < n; b++) {
        int a = 0, c = n - 1;

        while (a < c) {
            if (a == b) {
                a++;
                continue;
            }
            if (c == b) {
                c--;
                continue;
            }

            if (arr[a] + arr[c] == arr[b]) {
                count++;
                a++;
                c--;
            } else if (arr[a] + arr[c] < arr[b]) {
                a++;
            } else {
                c--;
            }
        }
    }

    return count;
}

int main() {
    freopen("PTB2.inp", "r", stdin);
    freopen("PTB2.out", "w", stdout);

    int n;
    cin >> n;
    int arr[n];

    for (int i = 0; i < n; ++i) {
        cin >> arr[i];
    }

    cout << dem(arr, n); 

    return 0;
}
Leave a Comment