Sort by

recency

|

1252 Discussions

|

  • + 0 comments

    def pairs(k, arr): arr_set = set(arr) return len({x + k for x in arr_set} & arr_set)

  • + 0 comments
    import java.util.*;
    
    public class Solution {
    
        public static void main(String[] args) {
            Scanner inr = new Scanner(System.in);
            int n = inr.nextInt(), k = inr.nextInt();
            int[] num = new int[n];
            for (int i = 0; i < n; i++) num[i] = inr.nextInt();
            Arrays.sort(num);
            int count = 0;
            for (int i = 0; i < n; i++) {
                for (int j = i + 1; j < n; j++) {
                    int diff = num[j] - num[i];
                    if (diff == k) count++;
                    if (diff > k) break;
                }
            }
            System.out.println(count);
        }
    }
    
  • + 0 comments
    import java.io.*;
    import java.math.*;
    import java.security.*;
    import java.text.*;
    import java.util.*;
    import java.util.concurrent.*;
    import java.util.function.*;
    import java.util.regex.*;
    import java.util.stream.*;
    import static java.util.stream.Collectors.joining;
    import static java.util.stream.Collectors.toList;
    
    class Result {
    
        /*
         * Complete the 'pairs' function below.
         *
         * The function is expected to return an INTEGER.
         * The function accepts following parameters:
         *  1. INTEGER k
         *  2. INTEGER_ARRAY arr
         */
    
        public static int pairs(int k, List<Integer> arr) {
        // Write your code here
      Set<Integer> set = new HashSet<>();
            int count = 0;
            for (int num : arr) {
                if (set.contains(num - k)) count++;
                if (set.contains(num + k)) count++;
                set.add(num);
            }
            return count;
        }
        }
    
    public class Solution {
        public static void main(String[] args) throws IOException {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
            BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
    
            String[] firstMultipleInput = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");
    
            int n = Integer.parseInt(firstMultipleInput[0]);
    
            int k = Integer.parseInt(firstMultipleInput[1]);
    
            List<Integer> arr = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
                .map(Integer::parseInt)
                .collect(toList());
    
            int result = Result.pairs(k, arr);
    
            bufferedWriter.write(String.valueOf(result));
            bufferedWriter.newLine();
    
            bufferedReader.close();
            bufferedWriter.close();
        }
    }
    
  • + 0 comments

    C:

    int comp(const void *a, const void *b) {
        return (*(int*)b - *(int*)a);
    }
    
    int pairs(int k, int arr_count, int* arr) {
        int p1 = 0;
        int p2 = 1;
        int count = 0;
        qsort(arr, arr_count, sizeof(int), comp);
        while(p2 < arr_count) {
            long long d = arr[p1] - arr[p2];
            ++p2;
            if(d == k) ++count;
            else if (d > k || p2 >= arr_count) {
                ++p1;
                p2 = p1 + 1;
            }
        }
        return count;
    }
    
  • + 2 comments

    Perl:

    sub pairs {
        my $k = shift;
        my $arr = shift;
        my $res = 0;
    
        my %h = map {$_ => 1} @$arr;
        foreach my $key (keys %h) {
            $res++ if (exists($h{$k + $key}));
        }
    
        return $res;
    }