by **ImplicitScheme** » Sun Jan 23, 2011 10:01 pm

For this problem

"

Sort a list of numbers in which each number is at a distance k from its actual position"

It seems this situation can only occur if the list has an even number of elements. I gave them a function like this.

void swap( vector<int>& v, int one, int two )

{

int temp1 = v[one] ;

v[one] =v[two] ;

v[two] = temp1 ;

} ;

void Koff(vector<int>& v, int k )

{

if((v.size())%2!=0 || k>2)

{

cout<< "FAILURE" ;

}

if(k==1)

for(int j = 0 ; j<v.size() -1 ; j=j+2)

swap(v,j,j+1) ;

if(k==2 && v.size()%4==0)

for(int j=0 ; j<v.size() -2; j++)

{

swap(v,j,j+2) ;

} ;

};

I'm not sure if I caught every possible case in there. I'm just curious if there is a better approach than this.