But not smart enough...took my Algorithms Analysis final earlier today and I still can't find the answer to this:
Array T of size n ints is considered a majority array if there is an element x in T such that x occurs more than n/2 times in the array. Write an algorithm that is linear time (theta n) that determines if the array is a majority or not. It does not have to be in place.
I made a second array (of size n/2) composed of pairs. One number for the element in T and the second for the number of occurences. I then updated the new array for each value in T and searched through it afterwards to see if any occurence value was > n/2.
Problem is, you have to iterate through the second array n/2 * n/2 = (n^2)/4 times for the worst case. No one in the class got the right answer and so I ask ATOT, how can you do this in linear time?
Example:
If T = (1,1,2,2,2,2,2,2,4,5) then C = {1,2},{2,6},{4,1},{5,1} and my algorithm would return true since 6 > (5 = n/2)
Array T of size n ints is considered a majority array if there is an element x in T such that x occurs more than n/2 times in the array. Write an algorithm that is linear time (theta n) that determines if the array is a majority or not. It does not have to be in place.
I made a second array (of size n/2) composed of pairs. One number for the element in T and the second for the number of occurences. I then updated the new array for each value in T and searched through it afterwards to see if any occurence value was > n/2.
Problem is, you have to iterate through the second array n/2 * n/2 = (n^2)/4 times for the worst case. No one in the class got the right answer and so I ask ATOT, how can you do this in linear time?
Example:
If T = (1,1,2,2,2,2,2,2,4,5) then C = {1,2},{2,6},{4,1},{5,1} and my algorithm would return true since 6 > (5 = n/2)