Problem · Array

Find Number of Interesting Pairs

Learn this problem
EasyThe D. E. Shaw Group logoThe D. E. Shaw GroupOA

Problem statement

A mathematics professor in a Senior Secondary High School decided to evaluate students for the Teachers Assessment based on their problem-solving skills.

Given an array of integers arr, and an integer, sumVal, the task is to pair the elements in arr into interesting pairs. Find the number of interesting pairs in the array. An unordered pair (i, j) is defined to be interesting if |arr[i] - arr[j]| + |arr[i] + arr[j]|, the sum of absolute difference and absolute sum at the values in respective indices, is equal to sumVal. The goal is to find the number of interesting pairs in the array.

Function

findNumberOfInterestingPairs(arr: int[], sumVal: int) → int

Examples

Example 1

arr = [1, 4, -1, 2]sumVal = 4return = 2
There are two interesting pairs, (1, 4) and (3, 4) because: |arr[1] - arr[4]| + |arr[1] + arr[4]| = |1 - 2| + |1 + 2| = 1 + 3 = 4. |arr[3] - arr[4]| + |arr[3] + arr[4]| = |-1 - 2| + |-1 + 2| = 3 + 1 = 4.

More The D. E. Shaw Group problems

drafts saved locally
public int findNumberOfInterestingPairs(int[] arr, int sumVal) {
    // write your code here
}
arr[1, 4, -1, 2]
sumVal4
expected2
checking account