Editorial for Last Digit Dominance


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Python:

n = int(input())
nums = list(map(int, input().split()))

nums.sort(key=lambda x: (x % 10, x))

print(*nums)

CPP:

#include <bits/stdc++.h>
using namespace std;

int main() {
    int n;
    cin >> n;

    vector<int> nums(n);
    for (int i = 0; i < n; i++) cin >> nums[i];

    sort(nums.begin(), nums.end(), [](auto& lhs, auto& rhs){
        if (lhs % 10 != rhs % 10) return lhs % 10 < rhs % 10;
        else return lhs < rhs;
    });

    for (int x : nums) cout << x << " ";
    cout << "\n";
}

Comments

There are no comments at the moment.