Yahoo Interview Question
Software Engineer / DevelopersThis is one of the solution, there is another simplier solution called "majority algorithm". It's quite simple the trick is if there exist a majority element, it will always be the candidate that return by the method "FindMajorityCandidate", in this case, we just need to look through the list again to see if that's a true Majority element or not (same as the second step mentioned above)
public int FindMajority(int* arr, int n)
{
int majorityCandidate = FindMajorityCandidate(arr,n);
// count whether the candidate is a real majority element;
int count = 0;
for(int i=0; i<n ;i++)
{
if( arr[i]==majorityCandidate )
{
count++;
}
}
if(count>n/2)
{
return majorityCandidate;
}
return -inf;
}
public int FindMajorityCandidate(int* arr, int n)
{
int majority;
int count = 0;
for(int i=0; i< n; i++)
{
if(majority == arr[i])
{
count++;
}else if(--count <= 0)
{
// change the majority candidate when the count for the current candidate become 0;
majority = arr[i];
count = 1;
}
}
}
This is one of the solution, there is another simplier solution called "majority algorithm". It's quite simple the trick is if there exist a majority element, it will always be the candidate that return by the method "FindMajorityCandidate", in this case, we just need to look through the list again to see if that's a true Majority element or not (same as the second step mentioned above)
public int FindMajority(int* arr, int n)
{
int majorityCandidate = FindMajorityCandidate(arr,n);
// count whether the candidate is a real majority element;
int count = 0;
for(int i=0; i<n ;i++)
{
if( arr[i]==majorityCandidate )
{
count++;
}
}
if(count>n/2)
{
return majorityCandidate;
}
return -inf;
}
public int FindMajorityCandidate(int* arr, int n)
{
int majority;
int count = 0;
for(int i=0; i< n; i++)
{
if(majority == arr[i])
{
count++;
}else if(--count <= 0)
{
// change the majority candidate when the count for the current candidate become 0;
majority = arr[i];
count = 1;
}
}
}
dude @cool..given that the elements may not be integers too in that the greater or less than comparison will not be possible..is it via ascii comparison????
<blockquote><code><font size="2" face="Courier New" color="black"><font color="#0000ff">int</font> getit(<font color="#0000ff">int</font> *ar, <font color="#0000ff">int</font> num)
{
<font color="#0000ff">int</font> full_pairs = (num + 1) / 2;
<font color="#0000ff">bool</font> odd_num_of_pairs = full_pairs % 2 ? <font color="#0000ff">true</font> : <font color="#0000ff">false</font>;
<font color="#0000ff">int</font> midle_index = num >> 1;
<font color="#0000ff">int</font> midle_item = ar[midle_index];
<font color="#0000ff">bool</font> left(<font color="#0000ff">false</font>);
<font color="#0000ff">if</font>(ar[midle_index - 1] == midle_item)
left = <font color="#0000ff">false</font>;
<font color="#0000ff">else</font> <font color="#0000ff">if</font>(ar[midle_index + 1] == midle_item)
left = <font color="#0000ff">true</font>;
<font color="#0000ff">else</font> <font color="#0000ff">return</font> midle_item;
<font color="#0000ff">if</font>((left && odd_num_of_pairs) || (!left && !odd_num_of_pairs))
getit(ar + midle_index, num - midle_index);
<font color="#0000ff">else</font> getit(ar, midle_index);
}
<font color="#0000ff">int</font> main()
{
<font color="#0000ff">const</font> <font color="#0000ff">int</font> N = 3;
<font color="#0000ff">int</font> ar[N] = {1, 2, 2};<font color="#008000">//, 2, 3, 3, 4, 4, 5, 6, 6}; </font>
std::cout << getit(ar, N);
}</font>
</blockquote>
<blockquote><code><font size="2" face="Courier New" color="black"><font color="#0000ff">int</font> getit(<font color="#0000ff">int</font> *ar, <font color="#0000ff">int</font> num)
{
<font color="#0000ff">int</font> full_pairs = (num + 1) / 2;
<font color="#0000ff">bool</font> odd_num_of_pairs = full_pairs % 2 ? <font color="#0000ff">true</font> : <font color="#0000ff">false</font>;
<font color="#0000ff">int</font> midle_index = num >> 1;
<font color="#0000ff">int</font> midle_item = ar[midle_index];
<font color="#0000ff">bool</font> left(<font color="#0000ff">false</font>);
<font color="#0000ff">if</font>(ar[midle_index - 1] == midle_item)
left = <font color="#0000ff">false</font>;
<font color="#0000ff">else</font> <font color="#0000ff">if</font>(ar[midle_index + 1] == midle_item)
left = <font color="#0000ff">true</font>;
<font color="#0000ff">else</font> <font color="#0000ff">return</font> midle_item;
<font color="#0000ff">if</font>((left && odd_num_of_pairs) || (!left && !odd_num_of_pairs))
getit(ar + midle_index, num - midle_index);
<font color="#0000ff">else</font> getit(ar, midle_index);
}
<font color="#0000ff">int</font> main()
{
<font color="#0000ff">const</font> <font color="#0000ff">int</font> N = 3;
<font color="#0000ff">int</font> ar[N] = {1, 2, 2};<font color="#008000">//, 2, 3, 3, 4, 4, 5, 6, 6}; </font>
std::cout << getit(ar, N);
}</font>
</code></blockquote>
if we remove 2 different elements from array say a and b and a != b then ans of remaining element i.e ans of array of size N-2 will be same as ans of N element array.
we can do this using one counter. and one variable.
int majority(int *a){
temp ;
count = 0;
for( int i = 0; i< N; i++){
if( count == 0) temp = a[i];
if( a[i] == temp)
count++;
else{
count--;
}
}
if(count > 0) return temp;
}
Note that if there exist a majority element in an array then that element must occur at A[n/2](convince yourself). So, if we can find element with rank n/2(say 'X') in the input array we can just scan the array to count the no .of times 'X' appears in it. If this count is greater than n/2 then it's the majority element else there's no majority element. It will be O(N). But the main problem is how do we find X??
- cool September 13, 2010For that there exists an O(N) time algorithm to find the element with a given rank in an array(Google for "linear time order statistic" or refer to CLRS).