Count of elements A[i] such that A[i] + 1 is also present in the Array
Last Updated :
14 Sep, 2021
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++
#include <bits/stdc++.h>
using namespace std;
int countElements( int * arr, int n)
{
int count = 0;
for ( int i = 0; i < n; i++) {
int x = arr[i];
int xPlusOne = x + 1;
bool found = false ;
for ( int j = i + 1; j < n; j++) {
if (arr[j] == xPlusOne) {
found = true ;
break ;
}
}
for ( int k = i - 1;
!found && k >= 0; k--) {
if (arr[k] == xPlusOne) {
found = true ;
break ;
}
}
if (found == true ) {
count++;
}
}
return count;
}
int main()
{
int arr[] = { 1, 2, 3 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << countElements(arr, n);
return 0;
}
|
Java
import java.io.*;
import java.util.Arrays;
class GFG{
public static int countElements( int [] arr, int n)
{
int count = 0 ;
for ( int i = 0 ; i < n; i++)
{
int x = arr[i];
int xPlusOne = x + 1 ;
boolean found = false ;
for ( int j = i + 1 ; j < n; j++)
{
if (arr[j] == xPlusOne)
{
found = true ;
break ;
}
}
for ( int k = i - 1 ; !found && k >= 0 ; k--)
{
if (arr[k] == xPlusOne)
{
found = true ;
break ;
}
}
if (found == true )
{
count++;
}
}
return count;
}
public static void main (String[] args)
{
int arr[] = { 1 , 2 , 3 };
int n = arr.length;
System.out.println(countElements(arr, n));
}
}
|
Python3
def countElements(arr,n):
count = 0
for i in range (n):
x = arr[i]
xPlusOne = x + 1
found = False
for j in range (i + 1 ,n, 1 ):
if (arr[j] = = xPlusOne):
found = True
break
k = i - 1
while (found = = False and k > = 0 ):
if (arr[k] = = xPlusOne):
found = True
break
k - = 1
if (found = = True ):
count + = 1
return count
if __name__ = = '__main__' :
arr = [ 1 , 2 , 3 ]
n = len (arr)
print (countElements(arr, n))
|
C#
using System;
class GFG{
static int countElements( int [] arr, int n)
{
int count = 0;
for ( int i = 0; i < n; i++) {
int x = arr[i];
int xPlusOne = x + 1;
bool found = false ;
for ( int j = i + 1; j < n; j++) {
if (arr[j] == xPlusOne) {
found = true ;
break ;
}
}
for ( int k = i - 1;
!found && k >= 0; k--) {
if (arr[k] == xPlusOne) {
found = true ;
break ;
}
}
if (found == true ) {
count++;
}
}
return count;
}
static public void Main ()
{
int [] arr = { 1, 2, 3 };
int n = arr.Length;
Console.WriteLine(countElements(arr, n));
}
}
|
Javascript
<script>
function countElements(arr, n)
{
let count = 0;
for (let i = 0; i < n; i++) {
let x = arr[i];
let xPlusOne = x + 1;
let found = false ;
for (let j = i + 1; j < n; j++)
{
if (arr[j] == xPlusOne)
{
found = true ;
break ;
}
}
for (let k = i - 1;
!found && k >= 0; k--) {
if (arr[k] == xPlusOne) {
found = true ;
break ;
}
}
if (found == true ) {
count++;
}
}
return count;
}
let arr = [ 1, 2, 3 ];
let n = arr.length;
document.write(countElements(arr, n));
</script>
|
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++
#include <bits/stdc++.h>
using namespace std;
int countElements(vector< int >& arr)
{
int size = arr.size();
int res = 0;
map< int , bool > dat;
for ( int i = 0; i < size; ++i) {
dat[arr[i] - 1] = true ;
}
for ( int i = 0; i < size; ++i) {
if (dat[arr[i]] == true ) {
res++;
}
}
return res;
}
int main()
{
vector< int > arr = { 1, 3, 2, 3, 5, 0 };
cout << countElements(arr) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG{
public static int countElements( int [] arr)
{
int size = arr.length;
int res = 0 ;
Map<Integer, Boolean> dat = new HashMap<>();
for ( int i = 0 ; i < size; ++i)
{
dat.put((arr[i] - 1 ), true );
}
for ( int i = 0 ; i < size; ++i)
{
if (dat.containsKey(arr[i]) == true )
{
res++;
}
}
return res;
}
public static void main(String[] args)
{
int [] arr = { 1 , 3 , 2 , 3 , 5 , 0 };
System.out.println(countElements(arr));
}
}
|
Python3
def countElements(arr):
size = len (arr)
res = 0
dat = {}
for i in range (size):
dat[arr[i] - 1 ] = True
for i in range (size):
if (arr[i] in dat):
res + = 1
return res
arr = [ 1 , 3 , 2 , 3 , 5 , 0 ]
print (countElements(arr))
|
C#
using System;
using System.Collections.Generic;
class GFG{
public static int countElements( int [] arr)
{
int size = arr.Length;
int res = 0;
Dictionary< int ,
Boolean> dat = new Dictionary< int ,
Boolean>();
for ( int i = 0; i < size; ++i)
{
if (!dat.ContainsKey(arr[i] - 1))
dat.Add((arr[i] - 1), true );
}
for ( int i = 0; i < size; ++i)
{
if (dat.ContainsKey(arr[i]) == true )
{
res++;
}
}
return res;
}
public static void Main(String[] args)
{
int [] arr = { 1, 3, 2, 3, 5, 0 };
Console.WriteLine(countElements(arr));
}
}
|
Javascript
<script>
function countElements(arr) {
var size = arr.length;
var res = 0;
var dat = new Map();
for (i = 0; i < size; ++i) {
dat.set((arr[i] - 1), true );
}
for (i = 0; i < size; ++i) {
if (dat.has(arr[i]) == true ) {
res++;
}
}
return res;
}
var arr = [ 1, 3, 2, 3, 5, 0 ];
document.write(countElements(arr));
</script>
|
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 the 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).
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...