Lexicographically largest string with sum of characters equal to N | C ++ program for the above approach ; Function to construct the lexicographically largest string having sum of characters as N ; Stores the resulting string ; Iterate until N is at least 26 ; Append ' z ' to the string ans ; Decrement N by 26 ; Append character at index ( N + ' a ' ) ; Return the resultant string ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string getString ( int N ) { string ans = " " ; while ( N >= 26 ) { ans += ' z ' ; N -= 26 ; } ans += char ( N + ' a ' - 1 ) ; return ans ; } int main ( ) { int N = 30 ; cout << getString ( N ) ; return 0 ; }