pranay_teja
10/2/2018 - 8:39 AM

Numbers ans String values

#include<bits/stdc++.h>
using namespace std;

// Numbers and string values
/*  
    Given two numbers n and k, find a string s of length n consisting of lowercase alphabets such that
    sum of all the elements in the string is equal to k. If there are multiple possibilities find 
    lexographically smallest one. a=1,b=2,... 
    Input:
    2
    5 42
    3 25
    Output:
    aaamz
    aaw
    
    */

string solve(int n, int k);

int main(){
    int t;
    cin>>t;
    while(t--){
        int n,k;
        cin>>n>>k;
        cout<<solve(n,k)<<endl;
    }
    
    return 0;
}

string solve(int n, int k){
    string ans;
    for(int i=0;i<n;i++){
        ans.push_back('a');
    }
    k-=n;
    int i=n-1;
    while(i>=0 && k>0){
        if(k>25){
            ans[i]+=25;
            i--;
            k-=25;
        }else{
            ans[i]+=k;
            break;
        }
    }
    return ans;
}