Please correct my code

since i want the sum to be odd it can be possible when two number is odd and even.
so i will take two number from the [1 N] and placed in the new vector and decrease k–

include <bits/stdc++.h>
using namespace std;

int main() {

int t;
cin>>t;
while(t–){
int n,k;
cin>>n>>k;
vectorv(n+1,false);
v[0]=true;
vectorresult;
while(k){
for(int i=1;i<v.size();i++){
if(v[i]==false and (result.empty()||i%2==1)){
result.push_back(i);
v[i]=true;
break;

        }

        if(v[i]==false and i%2==0){
            result.push_back(i);
            v[i]=true;
            k--;
            break;
        }
    }
    
}

for(int i=0;i<v.size();i++){
if(v[i]==false){
result.push_back(i);
}
}
for(int i=0;i<v.size();i++){
cout<<v[i]<<" ";
}
}
}
// but i am not getting correct output plz help me

@ankit1272003
which question ??

k odd sum

plzz send the problem link.

Can you share the link of the problem

1 Like

link K Odd Sum Practice Coding Problem - CodeChef

@ankit1272003
here plzz refer my c++ code for better understanding of the logic

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

int main() {
	// your code goes here
	int t;
	cin>>t;
	while(t--)
	{
	    int n,k;
	    cin>>n>>k;
	    for(int i=1;i<k;i++)
	    {
	        cout<<i<<" ";
	    }
	    cout<<k<<" ";
	    for(int i=k+2;i<=n;i+=2)
	    {
	        cout<<i<<" ";
	    }
	    for(int i=k+1;i<=n;i+=2)
	    {
	        cout<<i<<" ";
	    }
	    cout<<endl;
	}
}

include <bits/stdc++.h>
define ll long long
define pb push_back
define mod 1000000007;

using namespace std;

int main() {
// your code goes here
long long T;
cin>>T;
while(T–){
ll N,K;
cin>>N>>K;
long long i=1;
while(K>0){
cout<<i<<’ ‘;
i++;
K–;
}
long long j=i+1;
while(j<=N){
cout<<j<<’ ‘;
j+=2;
}
while(i<=N){
cout<<i<<’ ‘;
i+=2;
}
cout<<’\n’;
}
return 0;
}

1 Like

I think this code may help you, please go through the code.

1 Like