Question: Let be an array of size > = 1 , and 0 < = < . [ ] = 0 [ ] = 1 .
Let be an array of size and Decide which number is the
majority in without counting their quantities.
Write a program named in our language that can solve the above problem and bind the majority
among and to variable You can assume that is written in the memory state; and to simplify the
question, we artificially define so you dont need to worry about a possible runtime error when the
array index reaches Your program doesnt have to be deterministic. Be careful of the grammar in our
programming language.
Here are some hints:
We can use the following linearsearchlike algorithm: scan the array to pair up each with a Once we
have some left over, then is the majority; once we have some left over, then is the majority; if
we can pair up all numbers, then either of them can be the majority.
A student Jason wrote a partial solution that can be useful to solve the above problem:
Consider and as pointers for number and respectively. What program does is to find the next
and
such that and You can use inside of your program.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
