It fails for input 1,2,3. I think there needs to be one more iteration to check if the candidate is actually a majority element or not.
Thanks, Balaji. On Tue, Mar 15, 2011 at 9:50 PM, UTKARSH SRIVASTAV <[email protected]>wrote: > > CAN ANYONE PLEASE TELL ME WHY MY CODE IS GIVING WRONG ANSWER OR SOMEONE WHO > HAS GOT AC IN THIS PROBLEM MAY POST HIS SOLUTION > > #include<stdio.h> > main() > { > long long int n,t,r,count,major,i; > scanf("%lld",&t); > while(t--) > { > scanf("%lld",&n); > scanf("%lld",&r); > major=r; > count=1; > for(i=1;i<n;i++) > { > scanf("%lld",&r); > if(r!=major) > { > count--; > if(count<0) > { count=1; > major=r; > } > } > else > { > count++; > } > } > if(count<=0) > printf("NO\n"); > else > printf("YES%lld\n",major); > } > return 0; > } > > > > > > -- > *UTKARSH SRIVATAV* > *CSE-3 > B-Tech 2nd Year > @MNNIT ALLAHABAD* > > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
