Algorithm: First, identify one or two candidates for the majority element using
ID: 3743286 • Letter: A
Question
Algorithm: First, identify one or two candidates for the majority element using the process described below. If no candidates are identified during this step, then there is no majority in A, so stop. Otherwise, for each candidate, determine if it is actually the majority using a sequential search through the array To find the candidates in the array A, form a second array B. Then compare A 1 and A 2. If they are equal, add one of these to B; otherwise do nothing. Then compare A 3 and A 4. Again if they are equal, add one of these to B; otherwise do nothing. Continue in this fashion until the entire array is read. If the length of A is odd, then add the last element to B. Then recursively find the one or two majority candidates for B; these will be candidates for A.Explanation / Answer
public class FindCandidates { public static int[] findCandidates(int[] a) { if(a.lengthRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.