You are not logged in. Please login at www.codechef.com to post your questions!

×

Count Of Maximum C++ , Why is this getting WA , works fine with the given inputs. Can anyone find the problem here??

include<bits stdc++.h="">

using namespace std;

int main () { int k; cin>>k;

for(int j=0;j<k;j++){ int="" n;="" cin="">>n; int a[n];

for(int i=0;i<n;i++){
    cin>>a[i];
}

int maxCount=0; int number; int index=0;

for(int i=0;i<n;i++){

int count=0;

    for(int l=0;l<n;l++){
        if(a[i]==a[l]){

               count++;

                if(count>maxCount){
            maxCount=count;
            index=i;
    }
    if(count==maxCount){
        (a[i]>a[index])?number=a[index]:number=a[i];
    }
        }
    }



}

cout<<number<<" "<<maxCount<<endl; }

}

asked 06 Dec, 17:55

hhhgckid's gravatar image

0★hhhgckid
1
accept rate: 0%


TS : 8 1 10

Your Output : 8 1

Correct Ans : 1 1

(In case of ties in maximum frequency, select the smaller element.)

In maxcount==count case when you update your number you also need to change the index to i.

Just add index=i in false case in your ternary operator, you'll get AC.

link

answered 06 Dec, 19:26

black_truce's gravatar image

4★black_truce
814
accept rate: 33%

edited 07 Dec, 14:51

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×1,886
×84
×74
×41
×13

question asked: 06 Dec, 17:55

question was seen: 73 times

last updated: 07 Dec, 14:51