Open In App

Minimum number of letters needed to make a total of n

Improve
Improve
Like Article
Like
Save
Share
Report

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 <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));
    }
}


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 <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));
    }
}


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));
    }
}


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
?>


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 <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));
    }
}


Python3




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)


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.



Last Updated : 19 Sep, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads