Question: Let be an array of size > = 1 , and 0 < = < . [ ] = 0 [ ] = 1 .

Let be an array of size >=1, and 0<=<.[]=0[]=1. Decide which number (01) 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 0 and 1 to variable . You can assume that is written in the memory state; and to simplify the
question, we artificially define []=100, 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:
1) We can use the following linear-search-like algorithm: scan the array to pair up each 0 with a 1. Once we
have some 1 left over, then 1 is the majority; once we have some 0 left over, then 0 is the majority; if
we can pair up all numbers, then either of them can be the majority.
2) A student Jason wrote a partial solution that can be useful to solve the above problem:
[0]=1->00+1[1]=0->11+1
Consider 0 and 1 as pointers for number 0 and 1 respectively. What program does is to find the next
0 and 1
such that [0]=0 and [1]=1. 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 blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!