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 the 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 <bits/stdc++.h> ` `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<pair<` `int` `, ` `int` `> > 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; ` `} ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

**Output:**

12 31 10 18

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Sort the strings based on the numbers of matchsticks required to represent them
- Minimum count of numbers required from given array to represent S
- Sort an array of strings based on the frequency of good words in them
- Count of sticks required to represent the given string
- Count of matchsticks required to represent the given number
- Sort an array of strings based on count of distinct characters
- Put spaces between words starting with capital letters
- Regex in Python to put spaces between words starting with capital letters
- Count the number of carry operations required to add two numbers
- Minimum count of numbers required ending with 7 to sum as a given number
- Minimum count of numbers required with unit digit X that sums up to N
- Count words that appear exactly two times in an array of words
- Count of distinct numbers in an Array in a range for Online Queries using Merge Sort Tree
- Minimum Number of Platforms Required for a Railway/Bus Station | Set 2 (Map based approach)
- Represent N as sum of K even numbers
- Represent K^N as the sum of exactly N numbers
- Count of minimum reductions required to get the required sum K
- Minimum number of given powers of 2 required to represent a number
- Represent K as sum of N-bonacci numbers
- Sort the character array based on ASCII % N

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.