Sort the Array by reversing the numbers in it
Last Updated :
13 Apr, 2023
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++
// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
// Function to return the
// reverse of n
int reverseDigits(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(reverseDigits(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;
}
Java
// Java implementation of the approach
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG{
// Function to return the
// reverse of n
static int reverseDigits(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
static void sortArr(int arr[], int n)
{
// Vector to store the reverse
// with respective elements
ArrayList<int[]> vp = new ArrayList<>();
// Inserting reverse with elements
// in the vector pair
for(int i = 0; i < n; i++)
{
vp.add(new int[]{reversDigits(arr[i]),
arr[i]});
}
// Sort the vector, this will sort the pair
// according to the reverse of elements
Collections.sort(vp, (a, b) -> a[0] - b[0]);
// Print the sorted vector content
for(int i = 0; i < vp.size(); i++)
System.out.print(vp.get(i)[1] + " ");
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 12, 10, 102, 31, 15 };
int n = arr.length;
sortArr(arr, n);
}
}
// This code is contributed by offbeat
Python3
# Python3 implementation of the approach
# Function to return the
# reverse of n
def reverseDigits(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
C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
class GFG
{
// Function to return the
// reverse of n
static int reverseDigits(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
static void sortArr(int[] arr, int n)
{
// Vector to store the reverse
// with respective elements
List<Tuple<int, int>> vp = new List<Tuple<int, int>>();
// Inserting reverse with elements
// in the vector pair
for (int i = 0; i < n; i++)
{
vp.Add(new Tuple<int, int>(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 (int i = 0; i < vp.Count; i++)
Console.Write(vp[i].Item2 + " ");
}
// Driver code
static void Main()
{
int[] arr = { 12, 10, 102, 31, 15 };
int n = arr.Length;
sortArr(arr, n);
}
}
// This code is contributed by divyesh072019
JavaScript
<script>
// Javascript implementation of the
// above approach
// Function to return the
// reverse of n
function reverseDigits(num)
{
var rev_num = 0;
while (num > 0) {
rev_num = rev_num * 10 + num % 10;
num = Math.floor(num / 10);
}
return rev_num;
}
// Function to sort the array according to
// the reverse of elements
function sortArr(arr, n)
{
// Vector to store the reverse
// with respective elements
var vp = new Array(n);
for (var i = 0; i < n; i++) {
vp[i] = [];
}
// Inserting reverse with elements
// in the vector pair
for (var i = 0; i < n; i++) {
var pair = [];
pair.push(reversDigits(arr[i]));
pair.push(arr[i]);
vp[i] = pair;
}
// Sort the vector, this will sort the pair
// according to the reverse of elements
vp = vp.sort(function(a,b) {
return a[0] - b[0];
});
// Print the sorted vector content
for (var i = 0; i < n; i++){
document.write(vp[i][1] + " ");
}
}
// Driver code
var arr = [ 12, 10, 102, 31, 15 ];
var n = arr.length;
sortArr(arr, n);
// This code is contributed by Shivanisingh
</script>
Time Complexity:
O(N*log N), where N is the size of the array
Auxiliary Space: O(N)
Similar Reads
Check if reversing a sub array make the array sorted Given an array of n distinct integers. The task is to check whether reversing any one sub-array can make the array sorted or not. If the array is already sorted or can be made sorted by reversing any one subarray, print "Yes", else print "No". Examples: Input : arr [] = {1, 2, 5, 4, 3}Output : YesBy
15+ min read
Check if it is possible to sort the array after rotating it Given an array of size N, the task is to determine whether its possible to sort the array or not by just one shuffle. In one shuffle, we can shift some contiguous elements from the end of the array and place it in the front of the array.For eg: A = {2, 3, 1, 2}, we can shift {1, 2} from the end of t
8 min read
Sort the given Array in Spiral manner starting from the center Given an array arr[] of size N, the task is to sort the array in descending order starting from the mid of the array having the next largest element at the right of the middle element and the next largest at the left of the middle element and so on. Examples: Input: arr[] = {4, 9, 3, 5, 7}Output: 3
9 min read
Sort array by performing swapping under certain condition Given an array of positive elements arr[] of size N. You can select any index i such that i + 1 < N and swap arr[i], arr[i + 1] if they have the same number of set bits in their binary representation. The task is to determine whether it is possible to sort the array in ascending order by swapping
7 min read
Sort the given Array after sorting each number individually Given an array arr of size N, the task is to sort the digits of each element in the array and then sort the array in non-decreasing order. Print the array after sorting. Examples: Input: arr[] = {514, 34, 41, 39}Output: 41 43 93 541Explanation: Sorting each element of the array: arr[] = {145, 34, 14
6 min read