Sort integers in array according to their distance from the element K
Last Updated :
08 Apr, 2022
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 is at the 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 the 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++
#include <bits/stdc++.h>
using namespace std;
void distanceSort( int arr[], int K, int n)
{
vector< int > vd[n];
int pos;
for ( int i = 0; i < n; i++) {
if (arr[i] == K) {
pos = i;
break ;
}
}
int i = pos - 1, j = pos + 1;
vd[0].push_back(arr[pos]);
while (i >= 0) {
vd[pos - i].push_back(arr[i]);
--i;
}
while (j < n) {
vd[j - pos].push_back(arr[j]);
++j;
}
for ( int i = 0; i <= max(pos, n - pos - 1); ++i) {
sort(begin(vd[i]), end(vd[i]));
for ( auto element : vd[i])
cout << element << " " ;
}
}
int main()
{
int arr[] = {14, 1101, 10, 35, 0 }, K = 35;
int n = sizeof (arr) / sizeof (arr[0]);
distanceSort(arr, K, n);
return 0;
}
|
Java
import java.util.*;
class GFG{
@SuppressWarnings ( "unchecked" )
static void distanceSort( int arr[], int K, int n)
{
Vector vd[] = new Vector[n];
for ( int i = 0 ; i < n; i++)
{
vd[i] = new Vector();
}
int pos = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (arr[i] == K)
{
pos = i;
break ;
}
}
int i = pos - 1 , j = pos + 1 ;
vd[ 0 ].add(arr[pos]);
while (i >= 0 )
{
vd[pos - i].add(arr[i]);
--i;
}
while (j < n)
{
vd[j - pos].add(arr[j]);
++j;
}
for (i = 0 ; i <= Math.max(pos, n - pos - 1 ); ++i)
{
Collections.sort(vd[i]);
for (j = 0 ; j < vd[i].size(); j++)
{
int element = ( int )vd[i].get(j);
System.out.print(element + " " );
}
}
}
public static void main(String s[])
{
int arr[] = { 14 , 1101 , 10 , 35 , 0 };
int K = 35 ;
int n = arr.length;
distanceSort(arr, K, n);
}
}
|
Python3
def distanceSort(arr,K,n):
vd = [[] for i in range (n)]
for i in range (n):
if (arr[i] = = K):
pos = i
break
i = pos - 1
j = pos + 1
vd[ 0 ].append(arr[pos])
while (i > = 0 ):
vd[pos - i].append(arr[i])
i - = 1
while (j < n):
vd[j - pos].append(arr[j])
j + = 1
for i in range ( max (pos, n - pos - 1 ) + 1 ):
vd[i].sort(reverse = False )
for element in vd[i]:
print (element,end = " " )
if __name__ = = '__main__' :
arr = [ 14 , 1101 , 10 , 35 , 0 ]
K = 35
n = len (arr)
distanceSort(arr, K, n)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void distanceSort( int []arr,
int K, int n)
{
List< int > []vd = new List< int >[n];
int i ;
for (i = 0; i < n; i++)
{
vd[i] = new List< int >();
}
int pos = 0;
for (i = 0; i < n; i++)
{
if (arr[i] == K)
{
pos = i;
break ;
}
}
int j = pos + 1;
i = pos - 1;
vd[0].Add(arr[pos]);
while (i >= 0)
{
vd[pos - i].Add(arr[i]);
--i;
}
while (j < n)
{
vd[j - pos].Add(arr[j]);
++j;
}
for (i = 0; i <= Math.Max(pos,
n - pos - 1); ++i)
{
vd[i].Sort();
for (j = 0; j < vd[i].Count; j++)
{
int element = ( int )vd[i][j];
Console.Write(element + " " );
}
}
}
public static void Main(String []args)
{
int []arr = {14, 1101, 10, 35, 0};
int K = 35;
int n = arr.Length;
distanceSort(arr, K, n);
}
}
|
Javascript
<script>
function distanceSort(arr, K, n)
{
let vd = new Array(n);
for (let i = 0; i < n; i++)
{
vd[i] = new Array();
}
for (let i = 0; i < n; i++)
{
if (arr[i] == K)
{
pos = i
break
}
}
let i = pos - 1
let j = pos + 1
vd[0].push(arr[pos])
while (i >= 0){
vd[pos - i].push(arr[i])
i -= 1
}
while (j < n){
vd[j - pos].push(arr[j])
j += 1
}
for (let i = 0;i< Math.max(pos, n - pos - 1) + 1;i++){
vd[i].sort()
for (let element of vd[i])
document.write(element, " " )
}
}
let arr = [14, 1101, 10, 35, 0]
let K = 35
let n = arr.length
distanceSort(arr, K, n)
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...