Geeksforgeeks

X

DAYS

:

HOUR

:

MINS

:

SEC

Error

Copied to Clipboard

Given an array **Arr** of size **N**, swap the **K**_{th} element from beginning with **K**_{th} element from end.

**Example 1:**

**Input:
**N = 8, K = 3
Arr[] = {1, 2, 3, 4, 5, 6, 7, 8}
**Output: **1 2 6 4 5 3 7 8
**Explanation:** K_{th} element from beginning is
3 and from end is 6.

**Example 2:**

**Input:
**N = 5, K = 2
Arr[] = {5, 3, 6, 1, 2}
**Output:** 5 1 6 3 2
**Explanation: **K_{th} element from beginning is
3 and from end is 1.

**Your Task:**

You don't need to read input or print anything. Your task is to complete the function **swapKth()** which takes the array of integers **arr,** **n **and** k**** **as parameters and returns void. You have to modify the array itself.

**Expected Time Complexity:** O(1)

**Expected Auxiliary Space:** O(1)

**Constraints:**

1 ≤ K ≤ N ≤ 10^{5}

1 ≤ Arr[i] ≤ 10^{3}

We are replacing the old Disqus forum with the new Discussions section given below.

Click here to view old Disqus comments.

Click here to view old Disqus comments.

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

YesLoading...

Swap kth elements

...