FastPrepFastPrep
Problem Brief

Perfect Pairs

INTERNOA
See Snowflake online assessment and hiring insights

A pair of integers (x, y) is perfect if both of the following conditions are met:

  • min(|x - y|, |y - x|) ≤ min(|x|, |y|)
  • max(|x - y|, |y - x|) > max(|x|, |y|)
  • Given an array arr of length n, find the number of perfect pairs (arr[i], arr[j]) where 0 ≤ i < j < n.

    Here min(a, b) is the minimum of a and b, max(a, b) is the maximum of a and b, and |x| is the absolute value of x.

    Function Description

    Complete the function getPerfectPairsCount in the editor below.

    getPerfectPairsCount has the following parameter:

    • int arr[n]: an array of integers

    Returns

    int: the number of perfect pairs

    1Example 1

    Input
    arr = [2, 5, 3]
    Output
    2
    Explanation
    In this example, n = 3. The possible pairs are (2, 5), (2, 3), and (5, 3). (2, 5) is not perfect: min(|2 - 5|, |5 - 2|) = 3, min(|2|, |5|) = 2 It fails the first test: 3 > 2 (2, 3) is perfect: min(|2 - 3|, |3 - 2|) = 1, min(|2|, |3|) = 2 It passes the first test: 1 ≤ 2 max(|2 - 3|, |3 - 2|) = 1, max(|2|, |3|) = 3 It passes the second test: 1 < 3 (5, 3) is perfect min(|5 - 3|, |3 - 5|) = 2, min(|5|, |3|) = 3 It passes the first test: 2 ≤ 3 max(|5 - 3|, |3 - 5|) = 2, max(|5|, |3|) = 5 It passes the second test: 2 < 5 Therefore, the number of perfect pairs is 2.

    Constraints

    Limits and guarantees your solution can rely on.

  • 2 ≤ n ≤ 2*105
  • -109 ≤ arr[i] ≤ 109
  • public int getPerfectPairsCount(int[] arr) {
        // write your code here
    }
    
    Input

    arr

    [2, 5, 3]

    Output

    2

    Sign in to submit your solution.