Related Articles

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

• Last Updated : 18 Mar, 2021

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:

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)

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up