The power and simplicity of IComparer V

Easy problem but it shows the power of IComparer. There are multiple possible solutions here, hence you let the IComparer do the job by just comparing the absolute values in the (internal) QuickSort implementation. Code is down below, cheers, ACC.

Sort Array By Absolute Value - LeetCode

You are given an integer array nums.

Rearrange elements of nums in non-decreasing order of their absolute value.

Return any rearranged array that satisfies this condition.

Note: The absolute value of an integer x is defined as:

  • x if x >= 0
  • -x if x < 0

 

Example 1:

Input: nums = [3,-1,-4,1,5]

Output: [-1,1,3,-4,5]

Explanation:

  • The absolute values of elements in nums are 3, 1, 4, 1, 5 respectively.
  • Rearranging them in increasing order, we get 1, 1, 3, 4, 5.
  • This corresponds to [-1, 1, 3, -4, 5]. Another possible rearrangement is [1, -1, 3, -4, 5].

Example 2:

Input: nums = [-100,100]

Output: [-100,100]

Explanation:

  • The absolute values of elements in nums are 100, 100 respectively.
  • Rearranging them in increasing order, we get 100, 100.
  • This corresponds to [-100, 100]. Another possible rearrangement is [100, -100].

 

Constraints:

  • 1 <= nums.length <= 100
  • -100 <= nums[i] <= 100

public int[] SortByAbsoluteValue(int[] nums)
{
    Array.Sort(nums, new MyComparerAbsVal());
    return nums;
}

public class MyComparerAbsVal: IComparer
{
    public int Compare(Object x, Object y)
    {
        int ax = (int)x;
        if (ax < 0) ax = -ax;
        int ay = (int)y;
        if (ay < 0) ay = -ay;
        return ax.CompareTo(ay);
    }
}

Comments

Popular posts from this blog

Advent of Code - Day 6, 2024: BFS and FSM

Shortest Bridge – A BFS Story (with a Twist)

Prefix Sum VIII