518. Counting Elements

Given an integer array arr, count how many elements x there are, such that x + 1 is also in arr. If there are duplicates in arr, count them separately.

Example 1:

Input: arr = [4,5,6,7]

Output: 3

Explanation: 4, 5, and 6 are counted because 5, 6, and 7 are in arr.

Example 2:

Input: arr = [10,10,20,30]

Output: 0

Explanation: No numbers are counted because there is no 11, 21, or 31 in arr.

Now Your Turn!

Pick the correct output for the given input

Input: arr = [2,2,3,3,3,4,5,8]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= arr.length <= 1000
  • 0 <= arr[i] <= 1000

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
class Solution {
public:
int countElements(vector<int>& as) {
// Your code goes here
}
};
Test Case

Input:

Arr