GeeksforGeeks » Interview Questions
find the element appearing n times in an array of 2n elements
(17 posts)-
Given an array of 2n elements of which n elements are same and the remaining n elements are all different. Write a C program to find out the value which is present n times in the array
-
median :)
-
It is not median as array is not sorted. And sorting is not a good idea as expected time complexity is O(n).
-
We can apply hashing, as soon as we find an element which gets repeated, we will stop as all other elements are different.
Please correct me if i am wrong. -
hashing cannot be used because of collision problem. As n can be very large or other elements can be very large.
-
I have thought of an approach to the new problem (2n numbers).
Just remove the last element from the array and store in temp.
Now there are two cases.
Case1. The removed element is the element repeating n times.
That means (n-1) elements same and n elements different.
Case2. The removed element is normal element.
That means n elements same and n-1 different.Now, Just traverse the array using voting algorithm.You will get an element but not sure whether it is majority or not.
And we also have another element stored in temp.We are sure that ur desired element is either the temp or the element coming as candidate from the algorithm.
So traverse the array once again to count their frequency.
Print the desired element.Please correct me.
-
Hash can be used, keep increasing count of number index and any index count greater that 1 is repeated number n times. In worst case need to traverse n+1 times. complexity O(n) and space complexity O(n)
-
i think , finding n/2 smallest element will work...
using quick select we can find it easily...plz correct if mistaken...
-
no , it's not the n/2 element actually...
the repeating element will be N th or (N+1)th element which can be found using quick select ...
so if nth and n+1 th element are same ... we got the answer...but if both differ then we can find the maximum element and if it is equal to n+1 th element then it is the answer else nth element is the answer...
-
Create a binary tree with given array elements. Each node should have 2 int fields ,it's value and count (and for any node ,all left childs' values should be less and right childs' values should be grater). When duplicates are found increment the count field of the node.
Now, after creation of binary tree,just traverse the tree and check for which node count value is n.
-
None of the above solution are utilizing the fact : and the remaining n elements are all different.
-
take a set of ((total num of element)/(no of equal element) + 1), what happens is, of you have to keep n equal element in a set of 2n elements, there must be "atleast" one case where the particular element in question will come twice in consecutive 3 elements. So after making a set of 3 elements, check if any number is present twice in the set. If not, remove the first element and add the next element in the set and continue.
-
modified Moore voting theorem as we used to find majority element..isn't it..???
-
I am not able to understand why majority voting algorithm would not work. O(n) time solution without space.
-
@guest1: I think majority voting algorithms also will give a nice and small approach
below is the code#include<stdio.h> int array[] = {9,3,4,3,5,3,3,7}; void main() { int i = 0; int value[2] = {array[0],1}; int size = sizeof(array)/sizeof(int); for(i = 1;i < size;i++) { if(value[0] == array[i]) { value[1]++; } else { value[1]--; if(value[1] <= 0) { value[0] = array[i]; value[1] = 1; } } } printf("\nThe required element is %d\n",value[0]); } -
moore's algorithm is helpful when in a set of n elements an element is present n/2+1 times. So in our case it is not full proof....
read this post[commenst for solution to this particular problem]...
http://geeksforgeeks.org/?p=503 -
Using Voting Algorithm can work ...
1> It will return the maximum occurrence element ... o(N)
2> Now traverse again to find the count of that element obtained above ...
We can find out the count of that element tooo ....### Since it is given that N occurence of one element, therefore their should not be any element whose occurence is equals to N...
So time Complexity O(n) ...
Reply
You must log in to post.