Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

A majority element in an array, A , of size N is an element that appears more than N / 2 times ( thus ,

A majority element in an array, A, of size N is an element that appears more than N/2 times (thus, there is at most one).
For example, the array 3,3,4,2,4,4,2,4,4 has a majority element (4).
Conversely, the array 3,3,4,2,4,4,2,4 does not.
If there is no majority element, your program should indicate this. Here is a sketch of an algorithm to solve the problem:
First, a candidate majority element is found (this is the harder part). This candidate is the only element that could possibly be the majority element. The second step determines if this candidate is actually the majority. This is just a sequential search through the array. To find a candidate in the array, A, form a second array, B. Then compare A1 and A2. If they are equal, add one of these to B; otherwise do nothing. Then compare A3 and A4. Again, if they are equal, add one of these to B; otherwise do nothing. Continue in this fashion until the entire array is read. Then recursively find a candidate for B; this is the candidate for A (why?)
a. How does the recursion terminate?
b. How is the case where N is odd handled?
c. What is the running time of the algorithm?
d. How can we avoid using an extra array, B?
e. Write a program in pseudocode to compute the majority element.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Practical Neo4j

Authors: Gregory Jordan

1st Edition

1484200225, 9781484200223

More Books

Students also viewed these Databases questions

Question

1. How will you, as city manager, handle these requests?

Answered: 1 week ago