Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7783089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:44:27+00:00 2026-06-01T19:44:27+00:00

Im trying to write an algorithm to return true if a majority element exists

  • 0

Im trying to write an algorithm to return true if a majority element exists in an array and false otherwise.
edit: i can only tell if two elements are equal. meaning i can’t use < or >, only =.
edit: the solution should be in a divide-and-conquer method.
its runtime shouldnt go over nlogn, and i wrote something in java but im not sure if it is correct and how to calculate the runtime..
here is what i got:

public static boolean MajorityBoolean(int[] arr) {
    int c;
    int n = arr.length;
    for (int i = 0; i < n; i = i + 2) {
        System.out.println("*");
        if ((arr[i] == arr[(i + 1)%n]) || ((arr[i] == arr[(i - 1+n)%n]))) {
            c = 0;
            for (int j = 0; j < n; j = j + 1)
                if (arr[j] == arr[i])
                    c = c + 1;
            if (c > n / 2)
                return true;
        }
    }
    return false;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T19:44:29+00:00Added an answer on June 1, 2026 at 7:44 pm

    The runtime of the described algorithm is O(n^2). The outer loop is executed n/2 times, thus the inner counter j is resetted n/2 times, and thus the inner loop is executed total of O(n^2) times.

    I am not sure I am following the logic behind your idea, but here are two approaches how I’d implement it [in high level pseudo-code]:

    (1) create a histogram out of the data:

    • create a Map<Integer,Integer> [the key is the element and the value is the number of occurances]
    • iterate the array, and for each element count how many times it appears
    • iterate the histogram and find if there is a unique maxima.
    • If there is – return true, else return false.

    This approach is average of O(n) if you use a HashMap as the map.

    (2) sort and find max occurances:

    • Sort the array – as the result, all equal elements are adjacent to each other. You can use Arrays.sort(array) for sorting.
    • Count how many times each element appears [similar to the histogram idea], and find if there is a unique maxima. You don’t actually need to maintain the data for all elements, it’s enough to maintain for the top 2, and at the end to see if they are identical.

    This solution is O(nlogn) average + worst case [actually, depending on the sort – merge sort gives you O(nlogn) worst case, while quick-sort gives you O(n^2) worst case, and both are O(nlogn) on average case].

    EDIT:

    I misunderstood the problem at hand, I thought you are looking if there is a unique maxima. These 2 solutions still fits for the problem, you just need to modify the last step of each [to check if the most occuring element appears more then half of the times, which is again fairly easy and doable in O(n)].

    Also, there is another solution: Use selection algorithm to find the median, and after finding it, check if it is a majority element, and return if it is. Since selection algorithm is divide-and-conquer based solution, I think it fits your needs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write an algorithm to generate the ceiling panel from a horiontally
i am trying to write an algorithm for finding a sub matrix in a
I'm trying to write an algorithm that will let me iterate over all desired
I am trying to write an efficient algorithm that will effectively let me merge
I have been trying to write a shortest path algorithm, dijkstras algorithm, finding the
I am trying to write a small application using bouncycastle algorithm, from the BouncyCastleProvider.java
Trying to write a chat, like on facebook, I wondered if two clients can
I am currently trying to write an algorithm that determines how many bits are
I am trying to write a general function in F# that would return all
I am trying to write a generic Heap Sort algorithm. I get the following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.