Counting Elements LeetCode | Counting Elements LeetCode Java | Programming Tutorials

preview_player
Показать описание
Counting Elements LeetCode Solution using Java Code.

Given an integer array, count element x such that x + 1 is also in array.If there're duplicates in array, count them separately.

Example 1:

Input: {1, 2, 3}
Output: 2

Explanation:

First element is 1 + 1 = 2 ( 2 is present in an array)
Second element is 2 + 1 = 3 ( 3 is present in an array)
Third element is 3 + 1 = 4 ( 4 is not present in an array)


Example 2:

Input: {1, 1, 3, 3, 5, 5, 7, 7}
Output: 0

Example 3:

Input : {1, 3, 2, 3, 5, 0}
Output: 3

Explanation:
1 + 1 = 2 (Exist)
3 + 1 = 4 (Not exist)
2 + 1 = 3 (Exist)
3 + 1 = 4 (Not exist)
5 + 1 = 6 (Not exist)
0 + 1 = 1 (Exist)

Example 4:

Input : {1, 1, 2, 2}
Output: 2

This problem is the day 7 challenge of LeetCode 30 day challenge.

Рекомендации по теме
Комментарии
Автор

Neat and to the point explanation!
Thanks Sir :)

Neilblaze
Автор

Good explanation brother tnx keep doing this

naveenverma
Автор

Thanks for your video! Following is the solution in C++
int countElements(vector<int>& arr)
{
unordered_set<int> st;
vector<int>::iterator ptr;
unordered_set<int>::iterator s;
int count = 0;

for(ptr = arr.begin(); ptr != arr.end(); ptr++)
st.insert(*ptr);

for(ptr = arr.begin(); ptr != arr.end(); ptr++)
if (st.find(*ptr + 1) != st.end())
count++;

return count;
}

blindprogrammer
Автор

I'm trying to think of a faster way which uses only one run through of the array (O(n)).

Create HashMap of <Integer we've seen> <Integer's which complement integers we've seen>
for (int i = 0; i < arr.length; i++){
if (hashMap.contains(arr[i])){
total++;
}
hashMap.add(arr[i], arr[i] + 1);
hashMap.add(arr[i], arr[i] - 1);

}


return total;


of course if arr[i] is already part of the keys, prob dont need to add to save space.

maybe just do <integer we've seen> <array of integers which complement integers we've seen> to avoid adding twice

michaellieberman
Автор

how do you validate against empty input, to avoid format exception

chomuekez
Автор

Can you help me figure out why my code is not working in javascript?

function countElements(arr) {
let set = new Set()
let count = 0

for(let i = 0; i< arr.length; i++){
set.add(arr)
}
for(let i = 0; i < arr.length; i++){
if (set.has(arr[i] +1)){
count++
}
}
return count
}

omarz
Автор

Can we not use stacks for implementation ?

adityabej
Автор

no sir this code does not works for all the cases.. it cant pass the testcase of [1, 1, 2] . according to this code the answer would be 2 but the real answer is 1 for this.

amiteshrajvaidya
Автор

C# Code
//Runtime:92 ms
//Memory Usage: 23.9 MB
public int CountElements(int[] arr)
{
int counter = 0;
for (int i = 0; i < arr.Length; i++)
{ int j = 0;
while(j< arr.Length)
{
if(i!=j&& arr[i]+1== arr[j])
{
counter++;
break;
}
j++;
}
}
return counter;
}

Akashi
Автор

It does not work for the case arr =[1, 3, 2, 3, 5, 0] as ther are two 3's

rahullotlikar
join shbcf.ru