Question

Design an O(n) algorithm that determines whether or not there is a majority in a list...

Design an O(n) algorithm that determines whether or not there is a majority in a list of elements. For example, [3,2,1] is NO and [3,1,3] is YES.

I want an answer that doesn't require the use of dictionaries/hash maps. It cannot be "Moore's voting algorithm" or a variation of it. The algorithm also can not use linear sorting algorithms because the input can not be assumed to satisfy the required conditions for any of the linear sorting algorithms my professor discussed in class.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

public class Mejority {

   //driver program to test
   public static void main(String[] args) {
       int ar[]={1,2,3};
       mejority(ar);
       ar=new int[]{3,1,3};
       mejority(ar);

   }
   static void mejority(int ar[]){
      
       int size=max(ar); //size of new temp array as maximum element +1
       int temp[]=new int[size+1]; //new temp array
       for(int i=0;i<ar.length;i++){ //for each element of ar[i]
           temp[ar[i]]++; //suppose ar[i]=1 then increase the temp[1] element by 1 ie 1th count is now 1
                               //when next time ar[i] comes as 1 then this time temp[1] will be 2 ie count of 1 is 2 now
       }
       int mej=ar.length/2; //half of the length of original array
       for(int i=0;i<temp.length;i++){
          
           if(temp[i]>mej){ //if that element count greater then half of the length then print yes
               System.out.println("YES");
               return;
           }
       }
       System.out.println("NO");
   }
   //finds the max element is array
   static int max(int ar[]){
       int res=-1;
       for(int i=0;i<ar.length;i++){
           if(res<ar[i])
               res=ar[i];
       }
       return res;
   }
}

output

NO
YES

Add a comment
Know the answer?
Add Answer to:
Design an O(n) algorithm that determines whether or not there is a majority in a list...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need help In the lecture you got acquainted with the median algorithm, which calculates the median of an unsorted array with n∈N elements in O (n). But the algorithm can actually do much more: it is...

    I need help In the lecture you got acquainted with the median algorithm, which calculates the median of an unsorted array with n∈N elements in O (n). But the algorithm can actually do much more: it is not limited to finding only the median, but can generally find the ith element with 0≤i <n. Implement this generic version of the median algorithm by creating a class selector in the ads.set2.select package and implementing the following method: /** * Returns the...

  • I need the report like this (idea) *Sorting Algorithms: A sorting algorithm is an algorithm that...

    I need the report like this (idea) *Sorting Algorithms: A sorting algorithm is an algorithm that puts elements of a list in a certain order. The most-used orders are numerical order and lexicographical order. Efficient sorting is important for optimizing the use of other algorithms (such as search and merge algorithms) which require input data to be in sorted lists; it is also often useful for canonical zing data and for producing human-readable output. More formally, the output must satisfy...

  • import java.util.*; /** Description: This program determines the average of a list of (nonnegative) exam scores....

    import java.util.*; /** Description: This program determines the average of a list of (nonnegative) exam scores. Repeats for more exams until the user says to stop. Input: Numbers, sum, answer Output: Displays program description Displays instruction for user Displays average Algorithm: 1. START 2. Declare variables sum, numberOf Students, nextNumber, answer 3. Display welcome message and program description 4. DOWHILE user wants to continue 5. Display instructions on how to use the program 6. Inititalize sum and number of student...

  • Problem Set 2: Inheritance and Method Overriding The goal of this problem set is to apply...

    Problem Set 2: Inheritance and Method Overriding The goal of this problem set is to apply inheritance and method overriding in order to create a hierarchy of array sorters. There exist many algorithms to sort arrays. In this problem set, we are especially interested in “in-place” sorting algorithms like Selection Sort and Insertion Sort. In-place sorting refers to an approach in which we perform the sorting steps on the underlying array rather than using extra storage. When using object-oriented design,...

  • JAVA 3 LECTURE REVIEW PLEASE NEED ANSWERS ASAP. DUE IN AN HOUR!!! Question 12 points The...

    JAVA 3 LECTURE REVIEW PLEASE NEED ANSWERS ASAP. DUE IN AN HOUR!!! Question 12 points The best-case performance for a shell sort is: --- O(1) O(n2)   O(n) O(n log n)   Signaler cette question Question 22 points The best-case performance for an array of n items using insertion sort is: --- O(n2)   O(n) O(1) there is no best-case Signaler cette question Question 3 2 points A recursive method that processes a chain of linked nodes --- uses the first node in...

  • summatize the following info and break them into differeng key points. write them in yojr own...

    summatize the following info and break them into differeng key points. write them in yojr own words   apartus 6.1 Introduction—The design of a successful hot box appa- ratus is influenced by many factors. Before beginning the design of an apparatus meeting this standard, the designer shall review the discussion on the limitations and accuracy, Section 13, discussions of the energy flows in a hot box, Annex A2, the metering box wall loss flow, Annex A3, and flanking loss, Annex...

  • Risk management in Information Security today Everyday information security professionals are bombarded with marketing messages around...

    Risk management in Information Security today Everyday information security professionals are bombarded with marketing messages around risk and threat management, fostering an environment in which objectives seem clear: manage risk, manage threat, stop attacks, identify attackers. These objectives aren't wrong, but they are fundamentally misleading.In this session we'll examine the state of the information security industry in order to understand how the current climate fails to address the true needs of the business. We'll use those lessons as a foundation...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT