Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Given an array of integers, sort the array based on the absolute value of the elements.
- Input format
- First line contains an integer n - The number of elements.
- Second line contains n space separated integers - The array nums.
- Output format
- For each test case print the minimum changes required on a separate line.
- Sample Input 1
- 5
- 2 -5 1 -2 4
- Sample Output 1
- 1 2 -2 4 -5
- Explanation 1
- Absolute values of elements are [2,5,1,2,4] respectively, so in the sorted order based on absolute values elements will be [1,2,-2,4,-5] or [1,-2,2,4,-5]. Both are accepted answers.
- Constraints
- 1 <= n <= 10^5
- -10^9 <= nums[i] <= 10^9
- */
- function absoluteFuntion(n){
- return Math.abs(n);
- }
- /**
- * @param {number} n
- * @param {number[]} nums
- * @return {number[]}
- */
- function sortArrayAbsolute(n, nums) {
- nums.sort((a,b)=>{
- return absoluteFuntion(a)-absoluteFuntion(b);
- })
- return nums;
- }
- function main() {
- const n = parseInt(readLine(), 10)
- const nums = readIntArr()
- const result = sortArrayAbsolute(n, nums)
- console.log(result.join(" "))
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement