Help me in solving SUMARRAY problem

My issue

where is my wrong? I can’t find it.

My code

#include<bits/stdc++.h>
using namespace std;
#define ll long long
void solve()
{
    ll n,k;
    cin>>n>>k;

    ll x=n/2;

    if((x%2==1 && k%2==0) || (x%2==0 && k%2==1))
    {
        cout<<-1<<endl;
        return;
    }
    
    if(k%2==0)
    {
        ll sum=0;

        sum+=x;
        sum+=2*x;

        if(k<sum)
        {
            cout<<-1<<endl;
            return;
        }

        sum-=2;

        for(ll i=0;i<x;i++)
        cout<<1<<" ";
        for(ll i=x;i<n-1;i++)
        {
            cout<<2<<" ";
        }

        cout<<k-sum<<endl;

        return;
    }

    if(k%2==1)
    {
        ll sum=0;

        sum+=x;
        sum+=2*x;

        if(k<sum)
        {
            cout<<-1<<endl;
            return;
        }

        sum-=2;

        for(ll i=0;i<x;i++)
        cout<<1<<" ";
        for(ll i=x;i<n-1;i++)
        {
            cout<<2<<" ";
        }

        cout<<k-sum<<endl;

    }

}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        solve();
    }
}

Problem Link: Array Sum Practice Coding Problem - CodeChef

@pranto1610
plzz refer my c++ code

#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#include<algorithm>
#include<numeric>

using namespace std;
typedef long long int lli;
using namespace __gnu_pbds;
template<class T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
//typedef long long int lli;
//typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> multi_pbds;
//typedef tree<pair<int , int>, null_type, less<pair<int ,int>>, rb_tree_tag, tree_order_statistics_node_update> pbdsp;
//typedef tree< int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;
#define nl "\n";
#define fastio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define db long double
#define prq priority_queue<lli>
#define psq priority_queue<lli,vector<lli>,greater<lli>>
#define mod 1000000007
#define lb lower_bound
#define ub upper_bound
#define vlli vector<lli>
#define mslli multiset<lli>
#define inf 1e17
#define sp " "
#define pb push_back
#define pie 3.14159265358979323846
#define test lli t; cin>>t; while(t--)
int32_t main()
{
   #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    test{
        lli n,k;
        cin>>n>>k;
        lli n1=n;
        n=n/2;
        n=n*3;
        k-=n;

        if(k>=0&&k%2==0)
        {
            k=k/2;
            lli n2=n1/2;
            lli q=k/n2;
            lli r=k%n2;
            lli ch=0;
            if(r>0)
            ch=1;
            if(2+q+ch>100000)
            cout<<-1;
            else
            {
                for(int i=0;i<n1/2;i++)
                {
                    if(r>0)
                    {
                        cout<<1+q+1<<" ";
                        cout<<2+q+1<<" ";
                        r--;
                    }
                    else
                    {
                        cout<<1+q<<" "<<2+q<<" ";
                    }
                }
            }
            
        }
        else
            cout<<-1;
        cout<<endl;
    }

}
1 Like

Tnx.