# Lexicographically smallest string of length N and sum K

Given two integers N and K. The task is to print the lexicographically smallest string of length N consisting of lower-case English alphabates such that the sum of the characters of the string equals to K where ‘a’ = 1, ‘b’ = 2, ‘c’ = 3, ….. and ‘z’ = 26.

Examples:

Input: N = 5, K = 42
Output: aaamz
“aaany”, “babmx”, “aablz” etc. are also valid strings
but “aaamz” is the lexicographically smallest.

Input: N = 3, K = 25
Output: aaw

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

• Initialize char array of size N and fill all the element by ‘a’.
• Start traversing from the end of the array and replace the elements of the array by ‘z’ if K ≥ 26 or replace it by the character having ASCII value (K + 97 – 1).
• At the same time decrease the value of K by replaced element value i.e. for a = 1, b = 2, c = 3, …, z = 26.
• Also, note that we are subtracting previous element value i.e. (total ‘a’) before current element and adding the same before the end of for loop.
• Check for K < 0 condition and break the for loop.
• Return the new string formed by the elements of the char array as the answer.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to return the lexicographically ` `// smallest string of length n that ` `// satisfies the given condition ` `string lexo_small(``int` `n, ``int` `k) ` `{ ` `    ``string arr = ``""``; ` ` `  `    ``for``(``int` `i = 0; i < n; i++) ` `        ``arr += ``'a'``; ` ` `  `    ``// Iteration from the last position ` `    ``// in the array ` `    ``for` `(``int` `i = n - 1; i >= 0; i--) ` `    ``{ ` `        ``k -= i; ` ` `  `        ``// If k is a positive integer ` `        ``if` `(k >= 0) ` `        ``{ ` ` `  `            ``// 'z' needs to be inserted ` `            ``if` `(k >= 26) ` `            ``{ ` `                ``arr[i] = ``'z'``; ` `                ``k -= 26; ` `            ``} ` ` `  `            ``// Add the required character ` `            ``else` `            ``{ ` `                ``char` `c= (``char``)(k + 97 - 1); ` `                ``arr[i] = c; ` `                ``k -= arr[i] - ``'a'` `+ 1; ` `            ``} ` `        ``} ` ` `  `        ``else` `            ``break``; ` ` `  `        ``k += i; ` `    ``} ` `    ``return` `arr; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `n = 5, k = 42; ` ` `  `    ``string arr = lexo_small(n, k); ` ` `  `    ``cout << arr; ` `} ` ` `  `// This code is contributed by Mohit Kumar `

## Java

 `// Java implementation of the approach ` `import` `java.util.Arrays; ` ` `  `public` `class` `Main { ` ` `  `    ``// Function to return the lexicographically ` `    ``// smallest string of length n that ` `    ``// satisfies the given condition ` `    ``public` `static` `char``[] lexo_small(``int` `n, ``int` `k) ` `    ``{ ` `        ``char` `arr[] = ``new` `char``[n]; ` ` `  `        ``Arrays.fill(arr, ``'a'``); ` ` `  `        ``// Iteration from the last position ` `        ``// in the array ` `        ``for` `(``int` `i = n - ``1``; i >= ``0``; i--) { ` ` `  `            ``k -= i; ` ` `  `            ``// If k is a positive integer ` `            ``if` `(k >= ``0``) { ` ` `  `                ``// 'z' needs to be inserted ` `                ``if` `(k >= ``26``) { ` `                    ``arr[i] = ``'z'``; ` `                    ``k -= ``26``; ` `                ``} ` ` `  `                ``// Add the required character ` `                ``else` `{ ` `                    ``arr[i] = (``char``)(k + ``97` `- ``1``); ` `                    ``k -= arr[i] - ``'a'` `+ ``1``; ` `                ``} ` `            ``} ` ` `  `            ``else` `                ``break``; ` ` `  `            ``k += i; ` `        ``} ` ` `  `        ``return` `arr; ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``int` `n = ``5``, k = ``42``; ` ` `  `        ``char` `arr[] = lexo_small(n, k); ` ` `  `        ``System.out.print(``new` `String(arr)); ` `    ``} ` `} `

## Python3

 `# Python implementation of the approach ` ` `  `# Function to return the lexicographically ` `# smallest string of length n that ` `# satisfies the given condition ` `def` `lexo_small(n, k): ` ` `  `    ``arr ``=` `""; ` ` `  `    ``for` `i ``in` `range``(n): ` `        ``arr ``+``=` `'a'``; ` ` `  `    ``# Iteration from the last position ` `    ``# in the array ` `    ``for` `i ``in` `range``(n``-``1``,``-``1``,``-``1``): ` `        ``k ``-``=` `i; ` ` `  `        ``# If k is a positive integer ` `        ``if` `(k >``=` `0``): ` ` `  `            ``# 'z' needs to be inserted ` `            ``if` `(k >``=` `26``): ` `                ``arr ``=` `arr[:i] ``+` `'z'` `+` `arr[i``+``1``:]; ` `                ``k ``-``=` `26``; ` `         `  `            ``# Add the required character ` `            ``else``: ` `                ``c``=` `(k ``+` `97` `-` `1``); ` `                ``arr ``=` `arr[:i] ``+` `chr``(c) ``+` `arr[i``+``1``:]; ` `                ``k ``-``=` `ord``(arr[i]) ``-` `ord``(``'a'``) ``+` `1``; ` ` `  `        ``else``: ` `            ``break``; ` ` `  `        ``k ``+``=` `i; ` `    ``return` `arr; ` ` `  `# Driver code ` `if` `__name__ ``=``=` `'__main__'``: ` `    ``n ``=` `5``; k ``=` `42``; ` ` `  `    ``arr ``=` `lexo_small(n, k); ` ` `  `    ``print``(arr); ` ` `  `# This code contributed by PrinciRaj1992 `

## C#

 `// C# implementation of the approach  ` `using` `System; ` ` `  `class` `GFG ` `{  ` ` `  `    ``// Function to return the lexicographically  ` `    ``// smallest string of length n that  ` `    ``// satisfies the given condition  ` `    ``public` `static` `char``[] lexo_small(``int` `n, ``int` `k)  ` `    ``{  ` `        ``char` `[]arr = ``new` `char``[n];  ` `        ``int` `i; ` `         `  `        ``for``(i = 0; i < n; i++) ` `            ``arr[i] = ``'a'` `; ` ` `  `        ``// Iteration from the last position  ` `        ``// in the array  ` `        ``for` `(i = n - 1; i >= 0; i--)  ` `        ``{  ` `            ``k -= i;  ` ` `  `            ``// If k is a positive integer  ` `            ``if` `(k >= 0)  ` `            ``{  ` ` `  `                ``// 'z' needs to be inserted  ` `                ``if` `(k >= 26)  ` `                ``{  ` `                    ``arr[i] = ``'z'``;  ` `                    ``k -= 26;  ` `                ``}  ` ` `  `                ``// Add the required character  ` `                ``else` `                ``{  ` `                    ``arr[i] = (``char``)(k + 97 - 1);  ` `                    ``k -= arr[i] - ``'a'` `+ 1;  ` `                ``}  ` `            ``}  ` ` `  `            ``else` `                ``break``;  ` ` `  `            ``k += i;  ` `        ``}  ` `        ``return` `arr;  ` `    ``}  ` ` `  `    ``// Driver code  ` `    ``public` `static` `void` `Main()  ` `    ``{  ` `        ``int` `n = 5, k = 42;  ` ` `  `        ``char` `[]arr = lexo_small(n, k);  ` ` `  `        ``Console.WriteLine(``new` `string``(arr));  ` `    ``}  ` `} ` ` `  `// This code is contributed by AnkitRai01 `

Output:

```aaamz
```

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

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.

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.