Q:

Generate String equals to given sum within given character

Given two numbers n and k, find a string s of lowercase alphabets such that sum of values of all the elements in the string is equal to k. 
If there exist many such strings, find the one which is lexicographically the smallest.
The value of ith lowercase alphabet is i, for example, value of a is 1, b is 2, and so on.
Input format:
First line of input contain a single integer t, denoting number of test cases.
t lines follows each containing two space-separated integers n and k.
Output format:
Print the lexicographically smallest string of length n and having string sum value equal to k.
Constraints:
1 <= t <= 50
1 <= n <= 2*10^5
n <= k <= 26*n
Sample Input:
2
5 42
3 25
Sample Output:
aaamz
aaw
Explanation:
string value of aaamz is 42 also, it is smallest lexicographical string such that it contains 5 characters and has string value 42.

A:

generateLexicoString(int n, int k){
char[] a = new char[n];
    // Initially fill the array with character 'a'
    for(int i=0;i<n;i++){
        a[i] = 'a'; 
    }
    int a_count = n;
    int tot_count = k;
    int x = tot_count - a_count;
    for(int i=n-1;i>=0; i--){
        if(x> 26){
            a[i] = 'z';
            x = (x-26)+1;
        }
        else if(x > 0){
            a[i] = (char) (a[i] + x);
            x = 0;
        }
        if(x == 0)
            break;
    }
    String op="";
    for(int i=0;i<n;i++){
        op = op+a[i];
    }
    System.out.println(op);
}

