Monday, December 22, 2008

Finding Majority Element

Jargon: Majority Element is an element of an array that occurs more than half the number of times in the array. Also, [x] means smallest integer greater than x.

Easy Problem: Assume that an integer array A[1..n] has a majority element and elements other than majority element are distinct. How to find majority element. What's the space and time complexity of the algorithm ?

Answer: Time Complexity = n/2(comparisons), Space Complexity = O(1)

Solution: show

Hard Problem: Assume that an integer array A[1..n] has a majority element and elements other than majority element need NOT be distinct. How to find majority element. What's the space and time complexity of the algorithm?

Answer: Time Complexity=n(comparison), Space Complexity=O(1).

Solution: show

2 comments:

Hari said...

Super solution i like it :)

EternalBliss said...

beautiful soln....