# Sort numbers based on count of letters required to represent them in words

• Last Updated : 30 Sep, 2022

Given an array arr[] containing N non-negative integers, the task is to sort these integers according to the sum of the number of letters required to represent them.

Examples:

Input: arr[] = {12, 10, 31, 18}
Output: 12 31 10 18
Explanation:
12 -> one + two -> 3 + 3 = 6
31 -> three + one -> 4 + 3 = 7
10 -> one + zero -> 3 + 4 = 7
18 -> one + eight -> 3 + 5 = 8

Input: arr[] = {12, 10}
Output: 12 10
Explanation:
12 -> one + two -> 3 + 3 = 6
10 -> one + zero -> 3 + 4 = 7

Approach:

• Initially, an array of size ten is defined which contains the number of letters required to represent each digit.
• Now, the given array is iterated and for every number, the number of letters required to represent them is found.
• Now, this total count of characters and the number are added into a vector pair.
• Finally, this vector pair is sorted in ascending order.

Below is the implementation of the above approach:

## C++

 `// C++ program to sort the strings``// based on the numbers of letters``// required to represent them` `#include ``using` `namespace` `std;` `// letters[i] stores the count of letters``// required to represent the digit i``const` `int` `letters[] = { 4, 3, 3, 3, 4,``                        ``4, 3, 5, 5, 4 };` `// Function to return the sum of``// letters required to represent N``int` `sumOfLetters(``int` `n)``{``    ``int` `sum = 0;``    ``while` `(n > 0) {``        ``sum += letters[n % 10];``        ``n = n / 10;``    ``}``    ``return` `sum;``}` `// Function to sort the array according to``// the sum of letters to represent n``void` `sortArr(``int` `arr[], ``int` `n)``{``    ``// Vector to store the digit sum``    ``// with respective elements``    ``vector > vp;` `    ``// Inserting digit sum with elements``    ``// in the vector pair``    ``for` `(``int` `i = 0; i < n; i++) {` `        ``// Making the vector pair``        ``vp.push_back(``            ``make_pair(``                ``sumOfLetters(``                    ``arr[i]),``                ``arr[i]));``    ``}` `    ``// Sort the vector, this will sort the``    ``// pair according to the sum of``    ``// letters to represent n``    ``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, 31, 18 };``    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr[0]);` `    ``sortArr(arr, n);` `    ``return` `0;``}`

## Java

 `// Java program to sort the strings``// based on the numbers of letters``// required to represent them``import` `java.util.*;``import` `java.lang.*;` `class` `GFG{``    ` `// letters[i] stores the count of letters``// required to represent the digit i``static` `int` `letters[] = { ``4``, ``3``, ``3``, ``3``, ``4``,``                         ``4``, ``3``, ``5``, ``5``, ``4` `};``                         ` `// Function to return the sum of``// letters required to represent N``static` `int` `sumOfLetters(``int` `n)``{``    ``int` `sum = ``0``;``    ``while` `(n > ``0``)``    ``{``        ``sum += letters[n % ``10``];``        ``n = n / ``10``;``    ``}``    ``return` `sum;``}` `// Function to sort the array according to``// the sum of letters to represent n``static` `void` `sortArr(``int` `arr[], ``int` `n)``{``    ` `    ``// Vector to store the digit sum``    ``// with respective elements``    ``ArrayList<``int``[]> vp = ``new` `ArrayList<>();``    ` `    ``// Inserting digit sum with elements``    ``// in the vector pair``    ``for``(``int` `i = ``0``; i < n; i++)``    ``{``        ` `        ``// Making the vector pair``        ``vp.add(``new` `int``[]{sumOfLetters(arr[i]),``                                      ``arr[i]});``    ``}` `    ``// Sort the vector, this will sort the``    ``// pair according to the sum of``    ``// letters to represent n``    ``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``, ``31``, ``18` `};``    ``int` `n = arr.length;``    ` `    ``sortArr(arr, n);``}``}` `// This code is contributed by offbeat`

## Python3

 `# Python3 program to sort the strings``# based on the numbers of letters``# required to represent them` `# letters[i] stores the count of letters``# required to represent the digit i``letters ``=` `[``4``, ``3``, ``3``, ``3``, ``4``,``           ``4``, ``3``, ``5``, ``5``, ``4``]` `# Function to return the sum of``# letters required to represent N``def` `sumOfLetters(n):` `    ``sum` `=` `0``    ``while` `(n > ``0``):``        ``sum` `+``=` `letters[n ``%` `10``]``        ``n ``=` `n ``/``/` `10` `    ``return` `sum` `# Function to sort the array according to``# the sum of letters to represent n``def` `sortArr(arr, n):``    ` `    ``# List to store the digit sum``    ``# with respective elements``    ``vp ``=` `[]` `    ``# Inserting digit sum with elements``    ``# in the list pair``    ``for` `i ``in` `range``(n):``        ``vp.append([sumOfLetters(arr[i]), arr[i]])` `    ``# Sort the list, this will sort the``    ``# pair according to the sum of``    ``# letters to represent n``    ``vp.sort(key ``=` `lambda` `x : x[``0``])` `    ``# Print the sorted list content``    ``for` `i ``in` `vp:``        ``print``(i[``1``], end ``=` `" "``)` `# Driver code``if` `__name__ ``=``=` `'__main__'``:``    ``arr ``=` `[ ``12``, ``10``, ``31``, ``18` `]``    ``n ``=` `len``(arr)` `    ``sortArr(arr, n)` `# This code is contributed by Shivam Singh`

## C#

 `// C# program to sort the strings``// based on the numbers of letters``// required to represent them` `using` `System;``using` `System.Linq;``using` `System.Collections.Generic;` `class` `GFG {` `    ``// letters[i] stores the count of letters``    ``// required to represent the digit i``    ``static` `int``[] letters = { 4, 3, 3, 3, 4, 4, 3, 5, 5, 4 };` `    ``// Function to return the sum of``    ``// letters required to represent N``    ``static` `int` `sumOfLetters(``int` `n)``    ``{``        ``int` `sum = 0;``        ``while` `(n > 0) {``            ``sum += letters[n % 10];``            ``n = n / 10;``        ``}``        ``return` `sum;``    ``}` `    ``// Function to sort the array according to``    ``// the sum of letters to represent n``    ``static` `void` `sortArr(``int``[] arr, ``int` `n)``    ``{` `        ``// Vector to store the digit sum``        ``// with respective elements``        ``List<``int``[]> vp = ``new` `List<``int``[]>();` `        ``// Inserting digit sum with elements``        ``// in the vector pair``        ``for` `(``int` `i = 0; i < n; i++) {` `            ``// Making the vector pair``            ``vp.Add(``new``[] { sumOfLetters(arr[i]), arr[i] });``        ``}` `        ``// Sort the vector, this will sort the``        ``// pair according to the sum of``        ``// letters to represent n``        ``vp = vp.OrderBy(a => a[0]).ToList();` `        ``// Print the sorted vector content``        ``for` `(``int` `i = 0; i < vp.Count; i++)``            ``Console.Write(vp[i][1] + ``" "``);``    ``}` `    ``// Driver code``    ``public` `static` `void` `Main(``string``[] args)``    ``{``        ``int``[] arr = { 12, 10, 31, 18 };``        ``int` `n = arr.Length;` `        ``sortArr(arr, n);``    ``}``}` `// This code is contributed by phasing17`

## Javascript

 ``

Output:

`12 31 10 18`

Time Complexity: O(N * log(N)), where N is the size of the array.
Auxiliary Space: O(N) because extra space for vector vp is being used

My Personal Notes arrow_drop_up