Geeksforgeeks

Error

×

Leaderboard

Showing:

Handle | Score |
---|---|

@Ibrahim Nash | 6564 |

@blackshadows | 6400 |

@mb1973 | 5785 |

@Quandray | 5275 |

@akhayrutdinov | 5111 |

@saiujwal13083 | 5074 |

@kirtidee18 | 4639 |

@sanjay05 | 3762 |

@mantu_singh | 3712 |

@gfgaccount | 3627 |

@marius_valentin_dragoi | 3525 |

Complete Leaderboard | |

Handle | Score |

@yaswanthero123 | 625 |

@vikash619 | 611 |

@venkat11803610 | 501 |

@roshan13ar | 396 |

@DevangSingh | 379 |

@ravi11815721 | 312 |

@hrishipangavhane | 286 |

@aryanjain85297 | 280 |

@kommaraju11806903 | 277 |

@infosumitraut | 272 |

@ankitsharma5422 | 256 |

Complete Leaderboard |

Given an array of **N** elements and a number **K**. The task is to arrange array elements according to the absolute difference with K, i. e., element having minimum difference comes first and so on.

**Note :** If two or more elements are at equal distance arrange them in same sequence as in the given array.

**Example 1:**

**Input**: N = 5, K = 7
arr[] = {10, 5, 3, 9, 2}
**Output**: 5 9 10 3 2
**Explanation**: Sorting the numbers accoding to
the absolute difference with 7, we have
array elements as 5, 9, 10, 3, 2.

**Example 2:**

**Input**: N = 5, K = 6
arr[] = {1, 2, 3, 4, 5}
**Output**: 5 4 3 2 1
**Explanation**: Sorting the numbers according to
the absolute difference with 6, we have array
elements as 5 4 3 2 1.

**Your Task:**

This is a functional problem. You only need to complete the function **sortABS()**. The **printing **is done automatically by the **driver code**.

**Expected Time Complexity:** O(N log(N) ).

**Expected Auxiliary Space:** O(N).

**Constraints:**

1 <= N <= 10^{5}

1 <= K <= 10^{5}

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...

Sort by Absolute Difference

...