Before attempting this problem, you should be comfortable with:
Hash Set - Used for O(1) lookup to efficiently check if an element's successor exists
Array Traversal - Iterating through arrays and understanding linear search complexity
Sorting - Understanding how sorting brings related elements together for optimized approaches
1. Search with Array
Intuition
For each element x in the array, we need to check whether x + 1 also exists in the array. The straightforward approach is to scan through the entire array for each element to verify if its successor is present. While this works correctly, it requires a linear search for every element.
Algorithm
Initialize a counter to 0.
For each element x in the array, iterate through the array to check if x + 1 exists.
classSolution:defcountElements(self, arr: List[int])->int:
count =0for x in arr:if x +1in arr:
count +=1return count
classSolution{publicintcountElements(int[] arr){int count =0;for(int x : arr){if(integerInArray(arr, x +1)){
count++;}}return count;}publicbooleanintegerInArray(int[] arr,int target){for(int x : arr){if(x == target){returntrue;}}returnfalse;}}
classSolution{public:intcountElements(vector<int>& arr){int count =0;for(auto x : arr){if(integerInArray(arr, x +1)){
count++;}}return count;}boolintegerInArray(vector<int>& arr,int target){for(auto x : arr){if(x == target){returntrue;}}returnfalse;}};
classSolution{/**
* @param {number[]} arr
* @return {number}
*/countElements(arr){let count =0;for(const x of arr){if(arr.includes(x +1)){
count++;}}return count;}}
publicclassSolution{publicintCountElements(int[] arr){int count =0;foreach(int x in arr){if(IntegerInArray(arr, x +1)){
count++;}}return count;}privateboolIntegerInArray(int[] arr,int target){foreach(int x in arr){if(x == target){returntrue;}}returnfalse;}}
funccountElements(arr []int)int{
count :=0for_, x :=range arr {ifintegerInArray(arr, x+1){
count++}}return count
}funcintegerInArray(arr []int, target int)bool{for_, x :=range arr {if x == target {returntrue}}returnfalse}
class Solution {funcountElements(arr: IntArray): Int {var count =0for(x in arr){if(integerInArray(arr, x +1)){
count++}}return count
}privatefunintegerInArray(arr: IntArray, target: Int): Boolean {for(x in arr){if(x == target){returntrue}}returnfalse}}
classSolution{funccountElements(_ arr:[Int])->Int{var count =0for x in arr {if arr.contains(x +1){
count +=1}}return count
}}
Time & Space Complexity
Time complexity: O(N2)
Space complexity: O(1) constant space
Where N is the length of the input array arr.
2. Search with HashSet
Intuition
The brute force approach is slow because checking if x + 1 exists requires scanning the entire array. A hash set provides O(1) lookup time, so we can first store all elements in a set, then check for each element's successor in constant time.
Algorithm
Insert all elements from the array into a hash set.
Initialize a counter to 0.
For each element x in the array, check if x + 1 exists in the hash set.
classSolution{/**
* @param {number[]} arr
* @return {number}
*/countElements(arr){const hashSet =newSet(arr);let count =0;for(const x of arr){if(hashSet.has(x +1)){
count++;}}return count;}}
publicclassSolution{publicintCountElements(int[] arr){HashSet<int> hashSet =newHashSet<int>(arr);int count =0;foreach(int x in arr){if(hashSet.Contains(x +1)){
count++;}}return count;}}
funccountElements(arr []int)int{
hashSet :=make(map[int]bool)for_, x :=range arr {
hashSet[x]=true}
count :=0for_, x :=range arr {if hashSet[x+1]{
count++}}return count
}
class Solution {funcountElements(arr: IntArray): Int {val hashSet = arr.toHashSet()var count =0for(x in arr){if(hashSet.contains(x +1)){
count++}}return count
}}
classSolution{funccountElements(_ arr:[Int])->Int{let hashSet =Set(arr)var count =0for x in arr {if hashSet.contains(x +1){
count +=1}}return count
}}
Time & Space Complexity
Time complexity: O(N)
Space complexity: O(N)
Where N is the length of the input array arr.
3. Search with Sorted Array
Intuition
Sorting brings equal elements together and places consecutive values next to each other. After sorting, we can traverse the array once and track "runs" of identical values. When we encounter a new value that is exactly one more than the previous value, all elements in the previous run satisfy the condition.
Algorithm
Sort the array.
Initialize a counter and a run length tracker (starting at 1).
Iterate through the sorted array starting from index 1.
If the current element differs from the previous one:
If the current element equals the previous element plus 1, add the run length to the counter.
Reset the run length to 0.
Increment the run length for each element processed.
The overall space complexity is dependent on the space complexity of the sorting algorithm you're using. The space complexity of sorting algorithms built into programming languages are generally anywhere from O(N) to O(1).
Where N is the length of the input array arr.
Common Pitfalls
Checking for x - 1 Instead of x + 1
The problem asks to count elements x where x + 1 exists in the array. A common mistake is checking for x - 1 instead, which counts elements that have a predecessor rather than a successor.
# Wrong: Checking for predecessorif x -1in hash_set:
count +=1# Correct: Checking for successorif x +1in hash_set:
count +=1
Counting Unique Values Instead of All Occurrences
The problem asks to count every element x where x + 1 exists, including duplicates. Using a set for counting instead of iterating through the original array will miss duplicate elements.
# Wrong: Only counts unique values
hash_set =set(arr)
count =sum(1for x in hash_set if x +1in hash_set)# Correct: Count all occurrences including duplicates
hash_set =set(arr)
count =sum(1for x in arr if x +1in hash_set)