Skip to content

3847. Minimum Swaps To Sort By Digit Sum


3847. Minimum Swaps to Sort by Digit Sum

Medium


You are given an array nums of distinct positive integers. You need to sort the array in increasing order based on the sum of the digits of each number. If two numbers have the same digit sum, the smaller number appears first in the sorted order.

Return the minimum number of swaps required to rearrange nums into this sorted order.

A swap is defined as exchanging the values at two distinct positions in the array.

 

Example 1:

Input: nums = [37,100]

Output: 1

Explanation:

  • Compute the digit sum for each integer: [3 + 7 = 10, 1 + 0 + 0 = 1] → [10, 1]
  • Sort the integers based on digit sum: [100, 37]. Swap 37 with 100 to obtain the sorted order.
  • Thus, the minimum number of swaps required to rearrange nums is 1.

Example 2:

Input: nums = [22,14,33,7]

Output: 0

Explanation:

  • Compute the digit sum for each integer: [2 + 2 = 4, 1 + 4 = 5, 3 + 3 = 6, 7 = 7] → [4, 5, 6, 7]
  • Sort the integers based on digit sum: [22, 14, 33, 7]. The array is already sorted.
  • Thus, the minimum number of swaps required to rearrange nums is 0.

Example 3:

Input: nums = [18,43,34,16]

Output: 2

Explanation:

  • Compute the digit sum for each integer: [1 + 8 = 9, 4 + 3 = 7, 3 + 4 = 7, 1 + 6 = 7] → [9, 7, 7, 7]
  • Sort the integers based on digit sum: [16, 34, 43, 18]. Swap 18 with 16, and swap 43 with 34 to obtain the sorted order.
  • Thus, the minimum number of swaps required to rearrange nums is 2.

 

Constraints:

  • 1 <= nums.length <= 105
  • 1 <= nums[i] <= 109
  • nums consists of distinct positive integers.

Solution

class Solution {
    static class Tuple {
        int ele, sum, idx;
        public Tuple(int ele, int sum, int idx) {
            this.ele = ele;
            this.sum = sum;
            this.idx = idx;
        }
        @Override
        public String toString() {
            return "(" + ele + " " + sum + " " + idx + ")";
        }
    }
    static class custom_sort implements Comparator<Tuple> {
        @Override
        public int compare(Tuple first, Tuple second) {
            int op1 = Integer.compare(first.sum, second.sum);
            if (op1 != 0) return op1;
            return Integer.compare(first.ele, second.ele);
        }
    }
    public int minSwaps(int[] nums) {
        int n = nums.length;
        PriorityQueue<Tuple> pq = new PriorityQueue<>(new custom_sort());
        for (int i = 0; i < n; i++) {
            pq.offer(new Tuple(nums[i], compute_sum(nums[i]), i));
        }
        int count = 0, index = 0;
        int arr[] = new int[n];
        int loc[] = new int[n];
        while (pq.size() > 0) {
            int curr_ele = pq.peek().ele, curr_sum = pq.peek().sum , curr_idx = pq.peek().idx;
            arr[index++] = curr_idx;
            pq.poll();
        }
        int req[] = new int[n];
        for (int i = 0; i < n; i++) req[i] = arr[i];
        Arrays.sort(req);
        for (int i = 0; i < n; i++) loc[arr[i]] = i;
        for (int i = 0; i < n; i++) {
            if (arr[i] != req[i]) {
                count++;
                int idxx = loc[req[i]];
                loc[arr[i]] = idxx;
                int temp = arr[i];
                arr[i] = arr[idxx];
                arr[idxx] = temp;
            }
        }
        return count;
    }
    private int compute_sum(int n) {
        int res = 0;
        int temp = n;
        while (temp > 0) {
            res += temp % 10;
            temp /= 10;
        }
        return res;
    }
}

Complexity Analysis

  • Time Complexity: O(?)
  • Space Complexity: O(?)

Explanation

[Add detailed explanation here]