Lexicographically largest string with sum of characters equal to N | java 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
import java . io . * ; import java . lang . * ; import java . util . * ; public class GFG { static String getString ( int N ) { String ans = " " ; while ( N >= 26 ) { ans += ' z ' ; N -= 26 ; } ans += ( char ) ( N + ' a ' - 1 ) ; return ans ; } public static void main ( String [ ] args ) { int N = 30 ; System . out . print ( getString ( N ) ) ; } }