-4

I am using a divide and conquer strategy to solve the majority problem. An element in said to be in the majority if repeats more than n/2 times where n is the number of elements given in the input. Return 1 if a majority element in present, return 0 otherwise.

This is the algorithm that I am using:- ->We will keep dividing the array into half until we reach an array of size two and we will compare the two elements of each array. ->If they are the same, they are the majority element of that array and we will return their value. ->If they are not the same, we will return a special character to signify that there is no majority element. ->Moving recursively up the arrays, we will check the values we get from the two child-arrays/halves. As with the base case above, if the elements are the same, we return them, otherwise we return the special character(special character is -1 in the code below).

//Function to count the number of occurences of an element in the input in linear time
    ll simple_count(vector<ll>&v,ll p){
        ll count = 0;
        for(ll i=0;i<v.size();i++){
            if(v[i]==p)
                count++;
        }
        return count;
    }
    //Function to find the majority element using the above algorithm
    ll majorityElement(vector<ll>&v,ll left, ll right){
        if(left==right)
            return v[left];
        if(left+1==right){
            if(v[left]==v[right])
                return v[left];
            return -1;
        }
        ll mid = left+(right-left)/2;
        ll p = majorityElement(v,left,mid);
        ll q = majorityElement(v,mid+1,right);
        if(p!=-1&&q==-1)
            {
                if(simple_count(v,p)>((ll)v.size()/2))
                    return p;
                return -1;
            }
        else if(p==-1&&q!=-1)
            {
                if(simple_count(v,q)>((ll)v.size()/2))
                {
                    return q;
                }
                return -1;
            }
        else if(p!=-1&&q!=-1){
            ll p_count = simple_count(v,p);
            ll q_count = simple_count(v,q);
            if(p_count>q_count&&p_count>v.size()/2)
                return p;
            else if(q_count>p_count&&q_count>v.size()/2)
                return q;
            return -1;
        }
        else if(p==q&&p!=-1)
            {
            if(simple_count(v,p)>(ll)v.size()/2)
                return p;
            return -1;
            }
        else if(p==q)
            return -1;
    }
    int main(){
        ll n;
        cin>>n;
        vector<ll>v(n);
        for(ll i=0;i<n;i++)
            cin>>v[i];
        ll k = majorityElement(v,0,v.size()-1);
        if(k==-1)
            cout<<0;
        else
            cout<<1;
        return 0;
    }

The problem I am facing is that this code is giving me Time Limit Exceeded(TLE) error for a few inputs(Coursera grader does not share the inputs for which we get an error) I believe the time complexity of my code is O(nlogn). Please help me optimize the code so that this program does not give me TLE.

input format : the first line contains an integer n, followed by n non negative integers (a1,a2....an) . 1<=n<=10^5 and 0<=ai<=10^9. The array is not sorted by default

1 Answers1

3

You are not dividing. v is always the full array. simple_count does not take any divide indices to reduce where to search.

Therefore you do a full linear count in every call of majorityElement.

user4581301
  • 33,082
  • 7
  • 33
  • 54