Can this code be optimized?

the program is working fine but seems too long
can this be narrowed down?

hello @ashishxmathew
time complexity wise no.
but in terms on number of line yes.

check this->

else if((j>=(n-1+k )&& j<=(n-2+2*k))){

           cout<<(temp++ +1)<<" ";  

in this why are we couting +1 with temp??

temp++ is post increment operator .

value of temp will increment later thats why explicitly added +1 for printing.
basically that line is equivalent to
cout<<temp+1;
temp++;

if we use ++temp then it works the same?

no …

read difference between pre increment and post increment operator

i meant instead of temp++ +1 we can use ++temp.?? i tried it with the code its works ? is that correct ?

yeah thats correct … . . .