Minimum number of letters needed to make a total of n
Last Updated :
07 May, 2025
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: 2
48 can be written as z + v, where z = 26 and v = 22
Input: n = 23
Output: 1
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 <iostream>
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));
}
}
Python
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));
OutputMinimum number of letters needed: 2
Time Complexity: O(26^n)
Auxiliary Space: O(n)
Approach: There are 2 possible cases:
- If n is divisible by 26 then the answer will be n / 26.
- 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 <bits/stdc++.h>
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));
}
}
Python
# 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));
}
}
JavaScript
<script>
// Javascript implementation of the approach
// Function to return the minimum letters
// required to make a total of n
function minLettersNeeded(n)
{
if (n % 26 == 0)
return parseInt(n / 26);
else
return (parseInt(n / 26) + 1);
}
// Driver code
var n = 52;
document.write(minLettersNeeded(n));
// This code is contributed by noob2000
</script>
PHP
<?php
// PHP implementation of the approach
// Function to return the minimum
// letters required to make a
// total of n
function minLettersNeeded($n)
{
if ($n % 26 == 0)
return floor(($n / 26));
else
return floor(($n / 26) + 1);
}
// Driver code
$n = 52;
echo minLettersNeeded($n);
// This code is contributed by Ryuga
?>
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 <iostream>
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;
}
Java
import java.util.*;
public class GFG {
public static int minLetters(int n) {
return (n + 25) / 26;
}
public static void main(String[] args) {
int n = 48;
System.out.println("Minimum number of letters needed: " + minLetters(n));
}
}
Python
def minLetters(n):
return (n + 25) // 26 # Calculate the minimum number of letters needed
# Main function
def main():
n = 48
print("Minimum number of letters needed:", minLetters(n))
# Execute the main function
if __name__ == "__main__":
main()
C#
using System;
public class GFG
{
// Function to calculate the minimum number of letters needed
public static int MinLetters(int n)
{
return (n + 25) / 26;
}
public static void Main(string[] args)
{
int n = 48;
Console.WriteLine("Minimum number of letters needed: " + MinLetters(n));
}
}
JavaScript
// JavaScript code for above approach
// Function to calculate the minimum number of letters needed
function minLetters( n ) {
return (n + 25) / 26;
}
// Driver code
let n = 48;
let ans = Math.floor(minLetters(n));
console.log( "Minimum number of letters needed: "+ ans)
OutputMinimum 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.
Similar Reads
Minimum number of Appends needed to make a string palindrome
Given a string s, the task is to find the minimum characters to be appended (insertion at the end) to make a string palindrome. Examples: Input: s = "abede"Output : 2Explanation: We can make string palindrome as "abedeba" by adding ba at the end of the string.Input: s = "aabb"Output : 2Explanation:
12 min read
Minimum count of numbers needed from 1 to N that yields the sum as K
Given two positive integers N and K, the task is to print the minimum count of numbers needed to get the sum equal to K, adding any element from the first N natural numbers only once. If it is impossible to get the sum equal to K, then print "-1". Examples: Input: N = 5, K = 10Output: 3Explanation:
6 min read
Minimum numbers needed to express every integer below N as a sum
We have an integer N. We need to express N as a sum of K integers such that by adding some(or all) of these integers we can get all the numbers in the range[1, N]. What is the minimum value of K? Examples: Input : N = 7 Output : 3 Explanation : Three integers are 1, 2, 4. By adding some(or all) of t
5 min read
Minimum number of additions to make the string balanced
Given a string str of lowercase characters, the task is to find the minimum number of characters that need to add to the string in order to make it balanced. A string is said to be balanced if and only if the number of occurrences of each of the characters is equal. Examples: Input: str = "geeksforg
6 min read
Minimum count of elements that sums to a given number
Given infinite number of elements of form 10^n and 25*100^n ( n >= 0 ). The task is to find the minimum count of elements chosen such that there sum is equal to K. Examples: Input : K = 48 Output : 6 elements chosen are: (1 + 1 + 1 + 10 + 10 + 25)Input : 69 Output : 9 elements chosen are: (1 + 1
7 min read
Make n using 1s and 2s with minimum number of terms multiple of k
Given two positive integer n and k. n can be represented as the sum of 1s and 2s in many ways, using multiple numbers of terms. The task is to find the minimum number of terms of 1s and 2s use to make the sum n and also number of terms must be multiple of k. Print "-1", if no such number of terms ex
6 min read
Minimum Deletions to Make a String Palindrome
Given a string s of length n, the task is to remove or delete the minimum number of characters from the string so that the resultant string is a palindrome. Note: The order of characters should be maintained. Examples : Input : s = "aebcbda"Output : 2Explanation: Remove characters 'e' and 'd'. Resul
15+ min read
Minimum number of conversions to make the frequency of all characters odd
Given string str of lower case alphabets, the task is to output the minimum number of conversions to be made such that all characters are repeated an odd number of times, if the task is not possible, then print -1. Any alphabet can be converted into any other lower-case alphabet. Examples: Input: st
8 min read
Minimum number of power terms with sum equal to n
Given two positive integer n and x. The task is to express n as sum of powers of x (xa1 + xa2 +.....+ xa3) such that the number of powers of x (xa1, xa2, ....., xa3) should be minimum. Print the minimum number of power of x used to make sum equal to n.Examples: Input : n = 5, x = 3 Output : 3 5 = 30
6 min read
Minimum number of deletions so that no two consecutive are same
Given a string, find minimum number of deletions required so that there will be no two consecutive repeating characters in the string. Examples: Input : AAABBB Output : 4 Explanation : New string should be AB Input : ABABABAB Output : 0 Explanation : There are no consecutive repeating characters. If
4 min read