• + 1 comment

    Explanation goes:

    let we have n ants. We have to maximize the Hi among them so We have to divide them equally into two groups n*(n-n/2). Now Comes the main part We have Time= (10^9 + 6), so we will find the Encounter of ants only for 10^9 initially afterwords we count for 6. so
    Speed = 0.1 m/s ,
    Length of Track = 1000,
    so Time taken by ants to complete
    one cycle = 1000/0.1 = 10000.
    Total Time = 10^9
    Number of Cycles = 10^9/10^4 = 10^5
    Total Encounters = Number of Cycle * n*(n-n/2);

    Now Came The Second Part:

    The rest 6 sec. if the distance between 2 ants is <6*0.1*2 then Only there will be Encounter. so we Wil iterate through the initial Position of ants and check that whether Two ants are standing <1.2m distance or not, it they increase the Encounter +1;

    PS. Sort the Array and Check the last index and the first index are also at the distance less than 1.2 , then increase the Counter;

    Finally Return the total Encounter * 2 as in One Encounter there will be Two Hi

    Implementation in Cpp:

    nt solve(vector<int> V) {
        int n=V.size(),c=0;
        int x = 2*n/2*(n-n/2)*100000;
        sort(V.begin(),V.end());
        
        for(int i=0;i<n-1;i++)
        {
            if(V[i+1]-V[i]==1)
            {
                c++;
                i++;
            }
        }
        if(V[n-1]-V[0]==1)
            c++;
        x+=c;
        return x*2; 
    }