My issue
Here I was using a approach by printing “-1” if n is more than 2 times of k;
then according to the question half of the elements must be odd and other be even, i took half of the elements has 1 and the the other elements expect the last one as 0; and the last element was the difference of k and half of the elements which were 1;
My code
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin>>t;
while(t--){
int n,k;
int a[n];
cin>>n>>k;
if(n>2*k){
cout<<"-1"<<endl;
}
else{
for(int i=0;i<n/2;i++){
a[i]=1;
}
for(int i=n/2;i<n-1;i++){
a[i]=0;
}
a[n-1]=k-(n/2);
for(int i=0;i<n;i++){
cout<<a[i]<<" ";
}
cout<<endl;
}
}
return 0;
}
Problem Link: SUMARRAY Problem - CodeChef
@mayank_king
Your logic is not right bro.
plzz refer my solution for better understanding of logic and implementation.
And also keep in mind that we have to keep all A[i] between 1 and 10^5.
Here is my 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;
}
}
bro could you please explain your approach