Quick Overview

Count how many entries x of an integer array have x + 1 also present somewhere in the array, with duplicate copies of x counted separately. Tests hash set membership checks and careful reading of how duplicates are counted.

Count array elements whose successor x + 1 is also present

Company: DRW

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

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. ### Function Signature ```python def count_elements(arr: list[int]) -> int: ``` ### Rules - Every position of `arr` is checked on its own. An element `x` adds one to the count for each time it appears, as long as `x + 1` appears at least once anywhere in `arr`. - The number of copies of `x + 1` does not matter. Extra copies of `x + 1` do not add extra counts for `x`. ### Constraints - `1 <= arr.length <= 1000` - `0 <= arr[i] <= 1000` ### Examples ```text Input: arr = [1,2,3] Output: 2 ``` 1 and 2 are counted because 2 and 3 are in `arr`. 3 is not counted because there is no 4. ```text Input: arr = [1,1,3,3,5,5,7,7] Output: 0 ``` No numbers are counted, because there is no 2, 4, 6 or 8 in `arr`. ```text Input: arr = [1,1,2,2] Output: 2 ``` Both copies of 1 are counted because 2 is present. Neither copy of 2 is counted because 3 is absent, and the second copy of 2 does not count either 1 a second time.

Overview: Count how many entries x of an integer array have x + 1 also present somewhere in the array, with duplicate copies of x counted separately. Tests hash set membership checks and careful reading of how duplicates are counted.

Given an integer array `arr`, count how many elements `x` there are such that `x + 1` is also in `arr`. If `arr` contains duplicates, count them separately. Every position of `arr` is checked on its own. An element `x` adds one to the count for each time it appears, as long as the value `x + 1` appears at least once anywhere in `arr` (before or after `x`). The number of copies of `x + 1` does not matter: extra copies of `x + 1` do not add extra counts for `x`. Return the count as an integer. It is at most `arr.length`, so it never exceeds 1000 and fits in a 32-bit signed integer. ### Example 1 ```text Input: arr = [1, 2, 3] Output: 2 ``` 1 and 2 are counted because 2 and 3 are in `arr`. 3 is not counted because there is no 4. ### Example 2 ```text Input: arr = [1, 1, 2, 2] Output: 2 ``` Both copies of 1 are counted because 2 is present. Neither copy of 2 is counted because 3 is absent, and the second copy of 2 does not make 1 count a second time. ### Constraints - `1 <= arr.length <= 1000` - `0 <= arr[i] <= 1000` - `arr` may contain duplicates and need not be sorted.

Constraints

  • 1 <= arr.length <= 1000
  • 0 <= arr[i] <= 1000
  • arr may contain duplicates and need not be sorted

Examples

Input: ([1, 2, 3],)

Expected Output: 2

Explanation: Source example 1: 1 and 2 count because 2 and 3 are present, and 3 has no 4.

Input: ([1, 1, 3, 3, 5, 5, 7, 7],)

Expected Output: 0

Explanation: Source example 2: values separated by gaps of two, and none of 2, 4, 6 or 8 is present.

Hints

  1. Each position is judged on its own, so two copies of the same x can both count.
  2. Whether x counts depends only on whether the value x + 1 occurs at least once somewhere in the array. Its number of copies and its position play no part.
  3. Check your reasoning on [1, 1, 2, 2]: the answer is 2.

Count Elements Whose Successor Is Present

easyDRWSoftware Engineer

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

Every position of arr is checked on its own. An element x adds one to the count for each time it appears, as long as the value x + 1 appears at least once anywhere in arr (before or after x). The number of copies of x + 1 does not matter: extra copies of x + 1 do not add extra counts for x.

Return the count as an integer. It is at most arr.length, so it never exceeds 1000 and fits in a 32-bit signed integer.

Example 1

Input: arr = [1, 2, 3]
Output: 2

1 and 2 are counted because 2 and 3 are in arr. 3 is not counted because there is no 4.

Example 2

Input: arr = [1, 1, 2, 2]
Output: 2

Both copies of 1 are counted because 2 is present. Neither copy of 2 is counted because 3 is absent, and the second copy of 2 does not make 1 count a second time.

Constraints

  • 1 <= arr.length <= 1000
  • 0 <= arr[i] <= 1000
  • arr may contain duplicates and need not be sorted.

Examples

Example 1
Input
([1, 2, 3],)
Output
2
Notes
Source example 1: 1 and 2 count because 2 and 3 are present, and 3 has no 4.
Example 2
Input
([1, 1, 3, 3, 5, 5, 7, 7],)
Output
0
Notes
Source example 2: values separated by gaps of two, and none of 2, 4, 6 or 8 is present.

Constraints

  • •1 <= arr.length <= 1000
  • •0 <= arr[i] <= 1000
  • •arr may contain duplicates and need not be sorted

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def count_elements(arr: list[int]) -> int:
Sample call
count_elements([1, 2, 3])
Language
Case 1
Input
([1, 2, 3],)
Expected
2
Case 2
Input
([1, 1, 3, 3, 5, 5, 7, 7],)
Expected
0

Browse More Questions

Show the approach

Approach

Make two passes over arr. The first pass marks every value that occurs in a presence table indexed by value. The table has 1002 slots so that the lookup for x + 1 = 1001 (when x = 1000) stays in range and reads as absent. The second pass adds one for every position whose value x has x + 1 marked present.

Invariant: after the first pass, slot v is set exactly when v occurs somewhere in arr. The second pass then adds one per position i with arr[i] + 1 present, which is the definition of the count. Duplicates of x are visited once each, so each copy counts. Copies of x + 1 only set one flag, so their number never changes the result, and the position of x + 1 (before or after x) plays no part because the first pass finishes before any counting starts.

Edge cases: a single element gives 0, because x + 1 would have to be a second, different element. An array of equal values gives 0. The value 1000 never counts, since 1001 lies outside the value range.

Time complexity:
O(n + V), where V = 1001 is the size of the value range
Space complexity:
O(V)