#84. 数组差异计数

    ID: 84 Type: Default 1000ms 256MiB Tried: 0 Accepted: 0 Difficulty: (None) Uploaded By: Tags>codingbatArray-2gesp1循环结构条件结构

数组差异计数

Array Difference Count

Background

Dawei is organizing two sets of data. He wants to know how many pairs of corresponding elements in these two sets are "close but not identical".

Problem Description

Given arrays nums1 and nums2 of the same length, for every element in nums1, consider the corresponding element in nums2 (at the same index). Return the count of the number of times that the absolute difference between the two elements is less than or equal to 22, but they are not equal.

Input Format

Input is given from standard input in the following format.

One line, containing two integer lists enclosed in square brackets, with numbers in the lists separated by spaces. The first list represents nums1, and the second list represents nums2.

Output Format

Output is printed to standard output in the following format.

An integer, representing the count of pairs that satisfy the conditions.

Sample

[1 2 3] [2 3 10]
2
[1 2 3] [2 3 5]
3
[1 2 3] [2 3 3]
2

Sample Explanation

For Sample 1:

  • nums1[0]=1, nums2[0]=2. 12=1|1-2|=1, which satisfies 121 \le 2 and 121 \ne 2. Count.
  • nums1[1]=2, nums2[1]=3. 23=1|2-3|=1, which satisfies 121 \le 2 and 232 \ne 3. Count.
  • nums1[2]=3, nums2[2]=10. 310=7|3-10|=7, which does not satisfy 727 \le 2. Do not count. Total 2 pairs.

For Sample 3:

  • nums1[0]=1, nums2[0]=2. 12=1|1-2|=1, which satisfies 121 \le 2 and 121 \ne 2. Count.
  • nums1[1]=2, nums2[1]=3. 23=1|2-3|=1, which satisfies 121 \le 2 and 232 \ne 3. Count.
  • nums1[2]=3, nums2[2]=3. 33=0|3-3|=0, which satisfies 020 \le 2, but does not satisfy 333 \ne 3. Do not count. Total 2 pairs.

Constraints

Time limit for each test case is 1 second, and memory limit is 1024KB.