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: 2
48 can be written as z + v, where z = 26 and v = 22



Input: n = 23
Output: 1

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

   
# 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))

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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 contirbuted by Ryuga
?>

chevron_right


Output:

2

Time Complexity: O(1)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : AnkitRai01