Count of elements A[i] such that A[i] + 1 is also present in the Array
Given an integer array arr the task is to count the number of elements ‘A[i]’, such that A[i] + 1 is also present in the array.
Note: If there are duplicates in the array, count them separately.
Examples:
Input: arr = [1, 2, 3]
Output: 2
Explanation:
1 and 2 are counted cause 2 and 3 are in arr.Input: arr = [1, 1, 3, 3, 5, 5, 7, 7]
Output: 0
Approach 1: Brute Force Solution
For all the elements in the array, return the total count after examining all elements
- For current element x, compute x + 1, and search all positions before and after the current value for x + 1.
- If you find x + 1, add 1 to the total count
Below is the implementation of the above approach:
C++
// C++ program to count of elements // A[i] such that A[i] + 1 // is also present in the Array #include <bits/stdc++.h> using namespace std; // Function to find the countElements int countElements(int* arr, int n) { // Initialize count as zero int count = 0; // Iterate over each element for (int i = 0; i < n; i++) { // Store element in int x int x = arr[i]; // Calculate x + 1 int xPlusOne = x + 1; // Initialize found as false bool found = false; // Run loop to search for x + 1 // after the current element for (int j = i + 1; j < n; j++) { if (arr[j] == xPlusOne) { found = true; break; } } // Run loop to search for x + 1 // before the current element for (int k = i - 1; !found && k >= 0; k--) { if (arr[k] == xPlusOne) { found = true; break; } } // if found is true, increment count if (found == true) { count++; } } return count; } // Driver program int main() { int arr[] = { 1, 2, 3 }; int n = sizeof(arr) / sizeof(arr[0]); // call countElements function on array cout << countElements(arr, n); return 0; } |
Python3
# Python3 program to count of elements # A[i] such that A[i] + 1 # is also present in the Array # Function to find the countElements def countElements(arr,n): # Initialize count as zero count = 0 # Iterate over each element for i in range(n): # Store element in int x x = arr[i] # Calculate x + 1 xPlusOne = x + 1 # Initialize found as false found = False # Run loop to search for x + 1 # after the current element for j in range(i + 1,n,1): if (arr[j] == xPlusOne): found = True break # Run loop to search for x + 1 # before the current element k = i - 1 while(found == False and k >= 0): if (arr[k] == xPlusOne): found = True break k -= 1 # if found is true, increment count if (found == True): count += 1 return count # Driver program if __name__ == '__main__': arr = [1, 2, 3] n = len(arr) # call countElements function on array print(countElements(arr, n)) # This code is contributed by Surendra_Gangwar |
2
Time Complexity: In the above approach, for a given element, we check all other elements, So the time complexity is O(N*N) where N is no of elements.
Auxiliary Space Complexity: In the above approach, we are not using any additional space, so Auxiliary space complexity is O(1).
Approach 2: Using Map
- For all elements in the array, say x, add x-1 to the map
- Again, for all elements in the array, say x, check if it exists in the map. If it exists, increment the counter
- Return the total count after examining all keys in the map
Below is the implementation of the above approach:
// C++ program to count of elements // A[i] such that A[i] + 1 // is also present in the Array #include <bits/stdc++.h> using namespace std; // Function to find the countElements int countElements(vector<int>& arr) { int size = arr.size(); // Initialize result as zero int res = 0; // Create map map<int, bool> dat; // First loop to fill the map for (int i = 0; i < size; ++i) { dat[arr[i] - 1] = true; } // Second loop to check the map for (int i = 0; i < size; ++i) { if (dat[arr[i]] == true) { res++; } } return res; } // Driver program int main() { // Input Array vector<int> arr = { 1, 3, 2, 3, 5, 0 }; // Call the countElements function cout << countElements(arr) << endl; return 0; } |
3
Time Complexity: In the above approach, we iterate over the array twice. Once for filling the map and second time for checking the elements in map, So the time complexity is O(N) where N is no of elements.
Auxiliary Space Complexity: In the above approach, we are using an additional map which can contain N elements, so auxiliary space complexity is O(N).
Recommended Posts:
- Count the number of sub-arrays such that the average of elements present in the sub-array is greater than that not present in the sub-array
- Count elements present in first array but not in second
- Elements to be added so that all elements of a range are present in array
- Find elements which are present in first array and not in second
- Count pairs with average present in the same array
- Choose two elements from the given array such that their sum is not present in any of the arrays
- Sum of elements whose square root is present in the array
- Count number of elements between two given elements in array
- Count array elements that divide the sum of all other elements
- For each element in 1st array count elements less than or equal to it in 2nd array | Set 2
- For each element in 1st array count elements less than or equal to it in 2nd array
- Count distinct elements in an array
- Count number of even and odd elements in an array
- Count and Sum of composite elements in an array
- Count of greater elements for each element in the Array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : SURENDRA_GANGWAR

