Sort an array according to absolute difference with a given value “using constant extra space”
Last Updated :
30 Apr, 2021
Given an array of n distinct elements and a number x, arrange array elements according to the absolute difference with x, i. e., element having minimum difference comes first and so on, using constant extra space.
Note : If two or more elements are at equal distance arrange them in same sequence as in the given array.
Examples:
Input : arr[] = {10, 5, 3, 9, 2}
x = 7
Output : arr[] = {5, 9, 10, 3, 2}
Explanation :
7 - 10 = 3(abs)
7 - 5 = 2
7 - 3 = 4
7 - 9 = 2(abs)
7 - 2 = 5
So according to the difference with X,
elements are arranged as 5, 9, 10, 3, 2.
Input : arr[] = {1, 2, 3, 4, 5}
x = 6
Output : arr[] = {5, 4, 3, 2, 1}
The above problem has already been explained in a previous post here. It takes O(n log n) time and O(n) extra space. The below solution though has a relatively bad time complexity i.e O(n^2) but it does the work without using any additional space or memory.
The solution is a based on Insertion Sort . For every i (1<= i < n) we compare the absolute value of the difference of arr[i] with the given number x (Let this be ‘diff’ ). We then compare this difference with the difference of abs(arr[j]-x) where 0<= j < i (Let this if abdiff). If diff is greater than abdiff, we shift the values in the array to accommodate arr[i] in it’s correct position.
C++
#include <bits/stdc++.h>
using namespace std;
void arrange( int arr[], int n, int x)
{
for ( int i = 1; i < n; i++) {
int diff = abs (arr[i] - x);
int j = i - 1;
if ( abs (arr[j] - x) > diff) {
int temp = arr[i];
while ( abs (arr[j] - x) > diff && j >= 0) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = temp;
}
}
}
void print( int arr[], int n)
{
for ( int i = 0; i < n; i++)
cout << arr[i] << " " ;
}
int main()
{
int arr[] = { 10, 5, 3, 9, 2 };
int n = sizeof (arr) / sizeof (arr[0]);
int x = 7;
arrange(arr, n, x);
print(arr, n);
return 0;
}
|
Java
class GFG {
static void arrange( int arr[], int n, int x)
{
for ( int i = 1 ; i < n; i++) {
int diff = Math.abs(arr[i] - x);
int j = i - 1 ;
if (Math.abs(arr[j] - x) > diff)
{
int temp = arr[i];
while (j >= 0 && Math.abs(arr[j] - x) > diff)
{
arr[j + 1 ] = arr[j];
j--;
}
arr[j + 1 ] = temp;
}
}
}
static void print( int arr[], int n)
{
for ( int i = 0 ; i < n; i++)
System.out.print(arr[i] + " " );
}
public static void main(String[] args) {
int arr[] = { 10 , 5 , 3 , 9 , 2 };
int n = arr.length;
int x = 7 ;
arrange(arr, n, x);
print(arr, n);
}
}
|
Python 3
def arrange(arr, n, x):
for i in range ( 1 , n) :
diff = abs (arr[i] - x)
j = i - 1
if ( abs (arr[j] - x) > diff) :
temp = arr[i]
while ( abs (arr[j] - x) >
diff and j > = 0 ) :
arr[j + 1 ] = arr[j]
j - = 1
arr[j + 1 ] = temp
def print_1(arr, n):
for i in range (n):
print (arr[i], end = " " )
if __name__ = = "__main__" :
arr = [ 10 , 5 , 3 , 9 , 2 ]
n = len (arr)
x = 7
arrange(arr, n, x)
print_1(arr, n)
|
C#
using System;
class GFG
{
static void arrange( int []arr, int n, int x)
{
for ( int i = 1; i < n; i++)
{
int diff = Math.Abs(arr[i] - x);
int j = i - 1;
if (Math.Abs(arr[j] - x) > diff)
{
int temp = arr[i];
while (j >= 0 && Math.Abs(arr[j] - x) > diff)
{
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = temp;
}
}
}
static void print( int []arr, int n)
{
for ( int i = 0; i < n; i++)
Console.Write(arr[i] + " " );
}
public static void Main()
{
int []arr = { 10, 5, 3, 9, 2 };
int n = arr.Length;
int x = 7;
arrange(arr, n, x);
print(arr, n);
}
}
|
PHP
<?php
function arrange( $arr , $n , $x )
{
for ( $i = 1; $i < $n ; $i ++)
{
$diff = abs ( $arr [ $i ] - $x );
$j = $i - 1;
if ( abs ( $arr [ $j ] - $x ) > $diff )
{
$temp = $arr [ $i ];
while ( abs ( $arr [ $j ] - $x ) >
$diff && $j >= 0)
{
$arr [ $j + 1] = $arr [ $j ];
$j --;
}
$arr [ $j + 1] = $temp ;
}
}
return $arr ;
}
function print_arr( $arr , $n )
{
for ( $i = 0; $i < $n ; $i ++)
echo $arr [ $i ] . " " ;
}
$arr = array (10, 5, 3, 9, 2);
$n = sizeof( $arr );
$x = 7;
$arr1 = arrange( $arr , $n , $x );
print_arr( $arr1 , $n );
?>
|
Javascript
<script>
function arrange(arr,n,x)
{
for (let i = 1; i < n; i++) {
let diff = Math.abs(arr[i] - x);
let j = i - 1;
if (Math.abs(arr[j] - x) > diff)
{
let temp = arr[i];
while (j >= 0 &&
Math.abs(arr[j] - x) > diff)
{
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = temp;
}
}
}
function print(arr,n)
{
for (let i = 0; i < n; i++)
document.write(arr[i] + " " );
}
let arr=[10, 5, 3, 9, 2 ];
let n = arr.length;
let x = 7;
arrange(arr, n, x);
print(arr, n);
</script>
|
Output:
5 9 10 3 2
Time Complexity: O(n^2) where n is the size of the array.
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...