Ctrl K

Numbers With a Successor

Easy
You get an integer array values. Count the elements x for which x + 1 is also in the array. Repeated elements are each counted on their own.
Example 1
values=[4, 9, 5, 3] → 2

4 has a 5 and 3 has a 4; 9 and 5 have no successor. The answer is 2.

Example 2
values=[6, 6, 7, 2] → 2

Both 6s are counted separately, and each has a 7. The answer is 2.

Example 3
values=[0, 2, 4] → 0
Constraints
  • 1 ≤ len(values) ≤ 10⁵
  • 0 ≤ values[i] ≤ 1000
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsPair With a Target Sum EasyThe Candidate With Most Votes EasyDoes Any Value Repeat EasyRepeat Within Reach EasyThe Lost Ticket Easy
loading Python…
Input
values=[4, 9, 5, 3]
Expected
2

Run checks the first 3 tests in your browser. Submit checks all 121 tests on the server, hidden ones included.