Swap Kth Element from Beginning with Kth element from end

Swap Kth Element from Beginning with Kth element from end” is a basic problem of array data structure. Here, we are given an array of size ‘n’ and our task is to swap kth element from beginning with kth element from end, where (k<=n).

Example:

INPUT: 
Arr[10] = {11, 22, 33, 44, 55, 66, 77, 88, 99}
K = 4
OUTPUT:
Arr[10] = {11, 22, 33, 66, 55, 44, 77, 88, 99}

The steps required to swap kth element from beginning to kth element from end are as follows:

  1. Scan the size of array.
  2. Scan array elements.
  3. Swap (k-1)th element with (n-k-1)th
  4. Print the resultant array

C++ Program swap kth element from beginning with kth element from end is as follows:

/* Program to swap kth element from beginning to kth element from end */  
#include<bits/stdc++.h>  
using namespace std;  
int main()  
{  
    /* Scan array size */  
    int n;  
    cout<<"Enter array size: ";  
    cin>>n;  
      
    /* Scan array elements */  
    int arr[n];  
    cout<<"\nEnter array elements:";  
    for(int i = 0; i < n; i++)  
    cin>>arr[i];  
      
    /* Scan Kth Number */  
    int k;  
    cout<<"\nEnter kth number:";  
    cin>>k;  
      
    /* Printing array before swapping */  
    cout<<"\nArray before swapping:\n";  
    for(int i = 0; i < n; i++)  
    cout<<arr[i]<<" ";  
      
    /* Swap the number */  
    int temp = arr[k-1];  
    arr[k-1] = arr[n-k];  
    arr[n-k] = temp;  
      
    /* Printing array after swapping */  
    cout<<"\nArray after swapping:\n";  
    for(int i = 0; i < n; i++)  
    cout<<arr[i]<<" ";  
}  
OUTPUT:
Enter array size: 9 
Enter array elements:
11 22 33 44 55 66 77 88 99
Enter kth number: 9
Array before swapping:
11 22 33 44 55 66 77 88 99 
Array after swapping:
99 22 33 44 55 66 77 88 11

Time Complexity: 
O(1), for swapping numbers

Related Posts:

  1. Program to print all possible subarrays of the given array.
  2. Program to print kth smallest and kth largest element of an Array.
  3. Program to find equilibrium index of an Array.
  4. Program to find majority element of an Array.
  5. Program to find mean of the Array.
  6. Program to sort an Array of 0s and 1s.
  7. Program to Reverse an Array.
  8. Program to count number of odd numbers and even numbers in an array.
  9. Program to find absolute difference between sum of odd index elements and even index elements.
  10. Program to find smallest and largest element of the array.
  11. Program to count occurrences of a particular element in an array.
  12. Program to search an element in an Array (Linear Search).
  13. Program to Rotate Array Elements by ‘D’ positions.
  14. Program to find most frequent element in an array.
  15. Program to find pair in an array with given sum.
  16. Program to find first repeating element of an array.
  17. Program to merge two sorted arrays.
  18. Program to find missing number in an array.
  19. Program to sort if array is sorted.
  20. Program to print Alternate Elements of an Array.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *