Given an **array arr[]** of N integers and an **integer K**, the task is to sort these integers according to their distance from given integer **K**. If more than **1** element are at same distance, print them in increasing order.

**Note**: Distance between two elements in the array is measured as the difference between their index.

**Note :** The integer K is always present in array **arr[]** and is **unique**.

**Examples:**

Input:arr[] = {12, 10, 102, 31, 15}, K = 102

Output:102 10 31 12 15

Explanation:

Elements at their respective distance from K are,

At distance 0: 102

At distance 1: 10, 31 in sorted form.

At distance 2: 12, 15 in sorted form.

Hence, our resultant array is [ 102, 10, 31, 12, 15 ]

Input:arr[] = {14, 1101, 10, 35, 0}, K = 35

Output:35 0 10 1101 14

Explanation:

Elements at their respective distance from K are,

At distance 0: 35

At distance 1: 10, 0 and in sorted form we have 0, 10.

At distance 2: 1101

At distance 3: 14

Hence, our resultant array is [ 35, 0, 10, 1101, 14 ]

**Approach :**

To solve the problem mentioned above we create an auxiliary vector to store elements at any distance from K. Then find the position of given integer K in the array arr[] and insert the element K at position 0 in the auxiliary vector. Traverse the array in left direction from K and insert those elements in the vector at their distance from K. Repeat the above process for the right side elements of K. Finally, print the array elements from distance 0 in sorted order.

Below is the implementation of the above approach:

## C++

`// C++ implementation to Sort the integers in ` `// array according to their distance from given ` `// element K present in the array ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to get sorted array based on ` `// their distance from given integer K ` `void` `distanceSort(` `int` `arr[], ` `int` `K, ` `int` `n) ` `{ ` ` ` `// Vector to store respective elements ` ` ` `// with their distance from integer K ` ` ` `vector<` `int` `> vd[n]; ` ` ` ` ` `// Find the position of integer K ` ` ` `int` `pos; ` ` ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `if` `(arr[i] == K) { ` ` ` `pos = i; ` ` ` `break` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Insert the elements with their ` ` ` `// distance from K in vector ` ` ` ` ` `int` `i = pos - 1, j = pos + 1; ` ` ` ` ` `// Element at distnce 0 ` ` ` `vd[0].push_back(arr[pos]); ` ` ` ` ` `// Elements at left side of K ` ` ` `while` `(i >= 0) { ` ` ` `vd[pos - i].push_back(arr[i]); ` ` ` `--i; ` ` ` `} ` ` ` ` ` `// Elements at right side of K ` ` ` `while` `(j < n) { ` ` ` `vd[j - pos].push_back(arr[j]); ` ` ` `++j; ` ` ` `} ` ` ` ` ` `// Print the vector content in sorted order ` ` ` `for` `(` `int` `i = 0; i <= max(pos, n - pos - 1); ++i) { ` ` ` ` ` `// Sort elements at same distance ` ` ` `sort(begin(vd[i]), end(vd[i])); ` ` ` ` ` `// Print elements at distance i from K ` ` ` `for` `(` `auto` `element : vd[i]) ` ` ` `cout << element << ` `" "` `; ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `arr[] = {14, 1101, 10, 35, 0 }, K = 35; ` ` ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` ` ` `distanceSort(arr, K, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation to Sort the integers in ` `# array according to their distance from given ` `# element K present in the array ` ` ` `# Function to get sorted array based on ` `# their distance from given integer K ` `def` `distanceSort(arr,K,n): ` ` ` `# Vector to store respective elements ` ` ` `# with their distance from integer K ` ` ` `vd ` `=` `[[] ` `for` `i ` `in` `range` `(n)] ` ` ` ` ` `# Find the position of integer K ` ` ` ` ` `for` `i ` `in` `range` `(n): ` ` ` `if` `(arr[i] ` `=` `=` `K): ` ` ` `pos ` `=` `i ` ` ` `break` ` ` ` ` `# Insert the elements with their ` ` ` `# distance from K in vector ` ` ` ` ` `i ` `=` `pos ` `-` `1` ` ` `j ` `=` `pos ` `+` `1` ` ` ` ` `# Element at distnce 0 ` ` ` `vd[` `0` `].append(arr[pos]) ` ` ` ` ` `# Elements at left side of K ` ` ` `while` `(i >` `=` `0` `): ` ` ` `vd[pos ` `-` `i].append(arr[i]) ` ` ` `i ` `-` `=` `1` ` ` ` ` `# Elements at right side of K ` ` ` `while` `(j < n): ` ` ` `vd[j ` `-` `pos].append(arr[j]) ` ` ` `j ` `+` `=` `1` ` ` ` ` `# Print the vector content in sorted order ` ` ` `for` `i ` `in` `range` `(` `max` `(pos, n ` `-` `pos ` `-` `1` `) ` `+` `1` `): ` ` ` ` ` `# Sort elements at same distance ` ` ` `vd[i].sort(reverse` `=` `False` `) ` ` ` ` ` `# Print elements at distance i from K ` ` ` `for` `element ` `in` `vd[i]: ` ` ` `print` `(element,end ` `=` `" "` `) ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `arr ` `=` `[` `14` `, ` `1101` `, ` `10` `, ` `35` `, ` `0` `] ` ` ` `K ` `=` `35` ` ` ` ` `n ` `=` `len` `(arr) ` ` ` ` ` `distanceSort(arr, K, n) ` ` ` `# This code is contributed by Surendra_Gangwar ` |

*chevron_right*

*filter_none*

**Output:**

35 0 10 1101 14

**Time Complexity:** O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Find last element after deleting every second element in array of n integers
- Sort an Array of Points by their distance from a reference Point
- Check if it is possible to sort an array with conditional swapping of elements at distance K
- Maximum distance between two occurrences of same element in array
- Find the first repeating element in an array of integers
- Given an array and two integers l and r, find the kth largest element in the range [l, r]
- Sort the array according to their cubes of each element
- Sort an array with swapping only with a special element is allowed
- Count of smaller elements on right side of each element in an Array using Merge sort
- Minimum distance between the maximum and minimum element of a given Array
- Minimum distance between any most frequent and least frequent element of an array
- Find integral points with minimum distance from given set of integers using BFS
- Minimum product of k integers in an array of positive Integers
- Sort 3 Integers without using if condition or using only max() function
- Find Array formed by adding each element of given array with largest element in new array to its left
- Distance of closest zero to every element
- Add elements in start to sort the array | Variation of Stalin Sort
- Program to sort an array of strings using Selection Sort
- Bucket Sort To Sort an Array with Negative Numbers
- Largest element smaller than current element on left for every element in Array

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.