Count values close to a number
Count the elements whose distance from a given number is at most D, that is |a[i] − num| ≤ D. If none qualify, print -1.
Reported in: Accenture (Aug 2022), Deloitte (memory-based)
Input
Line 1: N. Line 2: N integers. Line 3: num. Line 4: D.
Output
The count, or -1.
Constraints
1 ≤ N ≤ 10^5; 0 ≤ D.
Example 1
Input:
5
12 3 14 56 77
13
2
Output:
2Example 2
Input:
3
1 2 3
10
1
Output:
-1Hint: Loop once and compare abs(a[i] − num) with D.