Can anyone tell me what's wrong with this solution?

Contest Name - Code-o-Fiesta
Problem Code: COFDQ1
Question link - https://www.codechef.com/COFDEC20/problems/COFDQ1/

#include<bits/stdc++.h>
#define int long long
using namespace std;

void fun(vector<int> &v,int k)
{
    for(int i=0;i<v.size()-k+1;i++)
    {
        vector<int> v1;
        for(int j=i;j<k+i;j++)
        {
            v1.push_back(v[j]);
        }

        sort(v1.begin(),v1.end());

        if(i==v.size()-k)
        {
            if(k%2==0)
            {
                int sum = v1[k/2]+v1[k/2-1];
                cout<<(float)sum/2;
            }
        else
        cout<<v1[k/2];    
        }
        else
        {
        if(k%2==0)
            {
                int sum = v1[k/2]+v1[k/2-1];
                cout<<(float)sum/2<<" ";
            }
        else
        cout<<v1[k/2]<<" ";
        }
    }

    cout<<endl;
    return;
}

int32_t main()
 {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

        int m;
        cin>>m;

        vector<int> v(m);

        for(int i=0;i<m;i++)
        cin>>v[i];

        int k;
        cin>>k;

        fun(v,k);

	return 0;
}

Instead of

write
cout<<(float)(sum/2.0)<<" ";

do same with the other cout also where you have used typecasting

It still doesn’t work.

#include<bits/stdc++.h>
#define int long long
using namespace std;

void fun(vector<int> &v,int k)
{
    for(int i=0;i<v.size()-k+1;i++)
    {
        vector<int> v1;
        for(int j=i;j<k+i;j++)
            v1.push_back(v[j]);

        sort(v1.begin(),v1.end());

        if(i==v.size()-k)
        {
            if(k%2==0)
            {
                int sum = v1[k/2]+v1[k/2-1];
                cout<<(float)(sum/2.0);
            }
        else
        cout<<v1[k/2];    
        }
        else
        {
        if(k%2==0)
            {
                int sum = v1[k/2]+v1[k/2-1];
                cout<<(float)(sum/2.0)<<" ";
            }
        else
        cout<<v1[k/2]<<" ";
        }
    }

    cout<<endl;
    return;
}

int32_t main()
 {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

        int m;
        cin>>m;

        vector<int> v(m);

        for(int i=0;i<m;i++)
        cin>>v[i];

        int k;
        cin>>k;

        fun(v,k);

	return 0;
}

It’s still tricky to find where it is going wrong. I just solved in Python 3.6, which uses same approach, which gave AC. I am not much aware of CPP, but surely, there should be something else apart from logic that is causing WA.
@ssjgz may help.

1 Like

Based on this AC solution, it might be as silly as requiring you to always output the decimal point whenever K is even e.g. for this test input:

4
1 4 4 2
4

the AC solution outputs 3.0.

1 Like