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
public class Main {
public static 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;
}
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 < = 0 ):
return 0 ;
if (n < = 26 ):
return 1 ;
minCount = n;
for i in range ( 1 , 27 ):
count = 1 + minLetters(n - i);
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 <= 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;
}
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:
- 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++
#include <bits/stdc++.h>
using namespace std;
int minLettersNeeded( int n)
{
if (n % 26 == 0)
return (n / 26);
else
return ((n / 26) + 1);
}
int main()
{
int n = 52;
cout << minLettersNeeded(n);
return 0;
}
|
Java
class GFG {
static int minLettersNeeded( int n)
{
if (n % 26 == 0 )
return (n / 26 );
else
return ((n / 26 ) + 1 );
}
public static void main(String args[])
{
int n = 52 ;
System.out.print(minLettersNeeded(n));
}
}
|
Python3
def minLettersNeeded(n):
if n % 26 = = 0 :
return (n / / 26 )
else :
return ((n / / 26 ) + 1 )
n = 52
print (minLettersNeeded(n))
|
C#
using System;
class GFG {
static int minLettersNeeded( int n)
{
if (n % 26 == 0)
return (n / 26);
else
return ((n / 26) + 1);
}
public static void Main()
{
int n = 52;
Console.Write(minLettersNeeded(n));
}
}
|
Javascript
<script>
function minLettersNeeded(n)
{
if (n % 26 == 0)
return parseInt(n / 26);
else
return (parseInt(n / 26) + 1);
}
var n = 52;
document.write(minLettersNeeded(n));
</script>
|
PHP
<?php
function minLettersNeeded( $n )
{
if ( $n % 26 == 0)
return floor (( $n / 26));
else
return floor (( $n / 26) + 1);
}
$n = 52;
echo minLettersNeeded( $n );
?>
|
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
def main():
n = 48
print ( "Minimum number of letters needed:" , minLetters(n))
if __name__ = = "__main__" :
main()
|
C#
using System;
public class GFG
{
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
function minLetters( n ) {
return (n + 25) / 26;
}
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.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
19 Sep, 2023
Like Article
Save Article