Officer_K's blog

By Officer_K, history, 7 years ago, In English

According to the solution to this problem , when we check a passing car, we should see if the number of appearances of cars with same color as the color of the car we check, is bigger than the number of appearances of the car with the color A and only then increase our counter for that car colour. Why we have to do that ?

here is my code which is obviously wrong

#include <iostream>

using namespace std;

int main(){
    int n, A;
    int cntA=0, cntB=0;
    int arr[100000];

    cin>>n>>A;

    int i,j;

    for(i=0;i<n;i++)
        cin>>arr[i];

    for(i=0;i<n;i++)
        if(arr[i]==A)
            cntA++;

    int max=0, mx, nr;
    for(i=0;i<n-1;i++){
        mx=1;
        for(j=i+1;j<n;j++)
            if(arr[i]==arr[j])
                mx++;
        if(mx>max){
            max=mx;
            nr=arr[i];
        }
    }

    if(cntA == 0)
        cout<<arr[0];//return anything
    else if(max>=cntA)
        cout<<nr;
    else
        cout<<-1;
}

Full text and comments »

  • Vote: I like it
  • -3
  • Vote: I do not like it