TechoSagar: Swap kth elements

Search

Google Alert - jobs

Swap kth elements

Given an array, swap kth element from beginning with kth element from end.
Input:
The first line of input contains an integer T denoting the number of test cases.
The first line of each test case is N and k,N is the size of array and kth number.
The second line of each test case contains N input C[i].

Output:
Print the modified array.

Constraints:
1 ≤ T ≤ 100
1 ≤ K ≤ N ≤ 500
1 ≤ C[i] ≤ 1000

Example:
Input
1
8 3
1 2 3 4 5 6 7 8
Output
1 2 6 4 5 3 7 8
Your code
#include<iostream>

using namespace std;

int main()
{
int t;
cin>>t;
while(t--)
{
int a[1000],i;
int n;
int k;
cin>>n>>k;;

for(i=0; i<n; i++)
{
cin>>a[i];
}
int temp;
temp=a[k-1];
a[k-1]=a[n-k];
a[n-k]=temp;
for(i=0; i<n; i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
}
}

Follow us

Follow Bijendra Kumar on Facebook