 GFG App
Open App Browser
Continue

# Minimum number of letters needed to make a total of n

Given an integer n and let a = 1, b = 2, c= 3, ….., z = 26. The task is to find the minimum number of letters needed to make a total of n.
Examples:

Input: n = 48
Output:
48 can be written as z + v, where z = 26 and v = 22

Input: n = 23
Output:

Brute Force:

We use recursion to find all possible combinations of letters that add up to the given number n. We start with the largest letter “z” and subtract its value from n, and then recursively call the function to find the minimum number of letters needed for the remaining value. We repeat this process for all letters from “z” to “a”, and return the minimum count of letters found.

## C++

 `#include ` `using` `namespace` `std;`   `int` `minLetters(``int` `n) {` `    ``if` `(n <= 0) {` `        ``return` `0;` `    ``}` `    ``if` `(n <= 26) {` `        ``return` `1;` `    ``}` `    ``int` `minCount = n;` `    ``for` `(``int` `i = 1; i <= 26; i++) {` `        ``int` `count = 1 + minLetters(n - i);` `        ``if` `(count < minCount) {` `            ``minCount = count;` `        ``}` `    ``}` `    ``return` `minCount;` `}`   `int` `main() {` `    ``int` `n = 48;` `    ``cout << ``"Minimum number of letters needed: "` `<< minLetters(n) << endl;`   `    ``return` `0;` `}`

## Java

 `// Java equivalent`   `public` `class` `Main {`   `    ``public` `static` `int` `minLetters(``int` `n) {` `        ``// If n is lower than 0, no letters are needed` `        ``if` `(n <= ``0``) {` `            ``return` `0``;` `        ``}` `        ``// If n is lower than 26, only one letter is needed` `        ``if` `(n <= ``26``) {` `            ``return` `1``;` `        ``}` `        ``int` `minCount = n;` `        ``// Iterate through 1 to 26` `        ``for` `(``int` `i = ``1``; i <= ``26``; i++) {` `            ``int` `count = ``1` `+ minLetters(n - i);` `            ``// If current count is lower than previous, set it as minCount` `            ``if` `(count < minCount) {` `                ``minCount = count;` `            ``}` `        ``}` `        ``return` `minCount;` `    ``}`   `    ``public` `static` `void` `main(String[] args) {` `        ``int` `n = ``48``;` `        ``System.out.println(``"Minimum number of letters needed: "` `+ minLetters(n));` `    ``}` `}`

## Python3

 `def` `minLetters(n) :` `    ``# If n is lower than 0, no letters are needed` `    ``if` `(n <``=` `0``):` `        ``return` `0``;` `    `  `    ``# If n is lower than 26, only one letter is needed` `    ``if` `(n <``=` `26``):` `        ``return` `1``;` `    `  `    ``minCount ``=` `n;` `    ``# Iterate through 1 to 26` `    ``for` `i ``in` `range``(``1``,``27``):` `        ``count ``=` `1` `+` `minLetters(n ``-` `i);` `        ``# If current count is lower than previous, set it as minCount` `        ``if` `(count < minCount):` `            ``minCount ``=` `count;` `        `  `    `  `    ``return` `minCount;`     `n ``=` `48``;` `print``(``"Minimum number of letters needed: "` `, minLetters(n));`

## C#

 `using` `System;`   `public` `class` `GFG {` `    ``public` `static` `int` `MinLetters(``int` `n)` `    ``{` `      `  `      ``// If n is lower than 0, no letters are needed` `        ``if` `(n <= 0) {` `            ``return` `0;` `        ``}` `      `  `      ``// If n is lower than 26, only one letter is needed` `        ``if` `(n <= 26) {` `            ``return` `1;` `        ``}` `        ``int` `minCount = n;` `        ``for` `(``int` `i = 1; i <= 26; i++) {` `            ``int` `count = 1 + MinLetters(n - i);` `          `  `          ``// If current count is lower than previous, set it as minCount` `            ``if` `(count < minCount) {` `                ``minCount = count;` `            ``}` `        ``}` `        ``return` `minCount;` `    ``}`   `    ``public` `static` `void` `Main()` `    ``{` `        ``int` `n = 48;` `        ``Console.WriteLine(` `            ``"Minimum number of letters needed: "` `            ``+ MinLetters(n));` `    ``}` `}`

## Javascript

 `function` `minLetters(n) {` `    ``if` `(n <= 0) {` `        ``return` `0;` `    ``}` `    ``if` `(n <= 26) {` `        ``return` `1;` `    ``}` `    ``let minCount = n;` `    ``for` `(let i = 1; i <= 26; i++) {` `        ``let count = 1 + minLetters(n - i);` `        ``if` `(count < minCount) {` `            ``minCount = count;` `        ``}` `    ``}` `    ``return` `minCount;` `}`   `let n = 48;` `console.log(``"Minimum number of letters needed: "` `+ minLetters(n));`

Output

`Minimum number of letters needed: 2`

Time Complexity: O(26^n)
Auxiliary Space: O(n)

Approach: There are 2 possible cases:

1. If n is divisible by 26 then the answer will be n / 26.
2. If n is not divisible by 26 then the answer will be (n / 26) + 1.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach` `#include ` `using` `namespace` `std;`   `// Function to return the minimum letters` `// required to make a total of n` `int` `minLettersNeeded(``int` `n)` `{` `    ``if` `(n % 26 == 0)` `        ``return` `(n / 26);` `    ``else` `        ``return` `((n / 26) + 1);` `}`   `// Driver code` `int` `main()` `{` `    ``int` `n = 52;` `    ``cout << minLettersNeeded(n);`   `    ``return` `0;` `}`

## Java

 `// Java implementation of the approach` `class` `GFG {`   `    ``// Function to return the minimum letters` `    ``// required to make a total of n` `    ``static` `int` `minLettersNeeded(``int` `n)` `    ``{` `        ``if` `(n % ``26` `== ``0``)` `            ``return` `(n / ``26``);` `        ``else` `            ``return` `((n / ``26``) + ``1``);` `    ``}`   `    ``// Driver code` `    ``public` `static` `void` `main(String args[])` `    ``{` `        ``int` `n = ``52``;` `        ``System.out.print(minLettersNeeded(n));` `    ``}` `}`

## Python3

 `# Python3 implementation of the approach`   `# Function to return the minimum letters` `# required to make a total of n` `def` `minLettersNeeded(n): `   `    ``if` `n ``%` `26` `=``=` `0``:` `        ``return` `(n``/``/``26``)` `    ``else``:` `        ``return` `((n``/``/``26``) ``+` `1``)`   `# Driver code` `n ``=` `52` `print``(minLettersNeeded(n))`

## C#

 `// C# implementation of the approach` `using` `System;` `class` `GFG {`   `    ``// Function to return the minimum letters` `    ``// required to make a total of n` `    ``static` `int` `minLettersNeeded(``int` `n)` `    ``{` `        ``if` `(n % 26 == 0)` `            ``return` `(n / 26);` `        ``else` `            ``return` `((n / 26) + 1);` `    ``}`   `    ``// Driver code` `    ``public` `static` `void` `Main()` `    ``{` `        ``int` `n = 52;` `        ``Console.Write(minLettersNeeded(n));` `    ``}` `}`

## PHP

 ``

## Javascript

 ``

Output

`2`

Time Complexity: O(1)
Auxiliary Space: O(1)

Approach:

This problem can be solved using simple arithmetic operations. Since there are 26 letters in the English alphabet, we can find the number of complete sets of 26 letters in the given number and add 1 to it if there are any remaining letters.

• In this approach, we simply add 25 to the given number and divide the result by 26 to get the minimum number of letters needed to make a total of n.
• The +25 is added to account for the case where there are remaining letters after forming complete sets of 26 letters.

Below is the implementation of the above approach:

## C++

 `#include ` `using` `namespace` `std;`   `int` `minLetters(``int` `n) {` `    ``return` `(n + 25) / 26;` `}`   `int` `main() {` `    ``int` `n = 48;` `    ``cout << ``"Minimum number of letters needed: "` `<< minLetters(n) << endl;`   `    ``return` `0;` `}`

Output

```Minimum number of letters needed: 2
```

Time Complexity: O(1), because the function minLetters() performs a constant number of arithmetic operations, regardless of the value of n.
Space Complexity: (1), as the we are not using any extra space.

My Personal Notes arrow_drop_up