Sort the Array by reversing the numbers in it

Given an array arr[] of N non-negative integers, the task is to sort these integers according to their reverse

Examples:

Input: arr[] = {12, 10, 102, 31, 15}
Output: 10 31 12 15 102
Reversing the numbers:
12 -> 21
10 -> 01
102 -> 201
31 -> 13
15 -> 51
Sorting the reversed numbers: 01 13 21 51 201
Original sorted array: 10 13 12 15 102



Input: arr[] = {12, 10}
Output: 10 12

Approach: The idea is to store each element with its reverse in a vector pair and then sort all the elements of the vector according to the reverse stored. Finally, print the elements in order.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to return the
// reverse of n
int reversDigits(int num)
{
    int rev_num = 0;
    while (num > 0) {
        rev_num = rev_num * 10 + num % 10;
        num = num / 10;
    }
    return rev_num;
}
  
// Function to sort the array according to
// the reverse of elements
void sortArr(int arr[], int n)
{
    // Vector to store the reverse
    // with respective elements
    vector<pair<int, int> > vp;
  
    // Inserting reverse with elements
    // in the vector pair
    for (int i = 0; i < n; i++) {
        vp.push_back(
            make_pair(reversDigits(arr[i]),
                      arr[i]));
    }
  
    // Sort the vector, this will sort the pair
    // according to the reverse of elements
    sort(vp.begin(), vp.end());
  
    // Print the sorted vector content
    for (int i = 0; i < vp.size(); i++)
        cout << vp[i].second << " ";
}
  
// Driver code
int main()
{
    int arr[] = { 12, 10, 102, 31, 15 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    sortArr(arr, n);
  
    return 0;
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach
  
# Function to return the
# reverse of n
def reversDigits(num) :
  
    rev_num = 0;
    while (num > 0) :
        rev_num = rev_num * 10 + num % 10;
        num = num // 10;
  
    return rev_num;
  
# Function to sort the array according to
# the reverse of elements
def sortArr(arr, n) :
  
    # Vector to store the reverse
    # with respective elements
    vp = [];
  
    # Inserting reverse with elements
    # in the vector pair
    for i in range(n) :
        vp.append((reversDigits(arr[i]),arr[i]));
  
    # Sort the vector, this will sort the pair
    # according to the reverse of elements
    vp.sort()
  
    # Print the sorted vector content
    for i in range(len(vp)) :
        print(vp[i][1],end= " ");
  
# Driver code
if __name__ == "__main__" :
  
    arr = [ 12, 10, 102, 31, 15 ];
    n = len(arr);
  
    sortArr(arr, n);
  
# This code is contributed by AnkitRai01

chevron_right


Output:

10 31 12 15 102

Time Complexity: O(N*log N), where N is the size of the array

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : AnkitRai01