Sort by

recency

|

1245 Discussions

|

  • + 0 comments

    Simple Python Solution:

    def pairs(k, arr):
        arr = set(arr)
        ans = 0
        for i in arr:
            if i + k in arr:
                ans += 1
        return ans
    
  • + 0 comments

    include

    using namespace std; int test(int n, long long k, vector a) { int dem = 0; unordered_sets(a.begin(), a.end()); for(int i = 0; i < n; i++) { if(a[i] > k){ long long tam = a[i] - k; if(s.find(tam) != s.end()) { dem++; } } } return dem; } int main() { int n; long long k; cin >> n >> k; vector a(n); for(int i = 0; i < n; i++) { cin >> a[i]; } cout << test(n, k, a); return 0; }

  • + 0 comments

    C#

    public static int pairs(int k, List<int> arr)
    {
        var hashSet = new HashSet<int>(arr);
        return hashSet.Count(x => hashSet.Contains(x + k));
    }
    
    
    public static int pairs(int k, List<int> arr)
    {
        var total = 0;
        var hashSet = new HashSet<int>(arr);
    
        foreach (var n in hashSet)
        {
            if (hashSet.Contains(n + k))
            {
                total++;
            }
        }
    
        return total;
    }
    
  • + 0 comments

    Python 3:

    from collections import Counter

    counter = Counter(arr) num_pairs = 0

    for i in arr: if i - k in counter: num_pairs += 1 # Note: integers in arr are distinct

    return num_pairs

  • + 0 comments

    Simple C++ sol using set | O(n)

    `c++

    int pairs(int k, vector arr) { unordered_set st; int n = arr.size(), c = 0; for (int i=0; i

    `