COAD01 - Editorial

Problem Code: COAD01

Problem Link is here

Problem Name: Beautiful Function

Contest Name: CodeAdda Practice Contest – 2 (CPC2016)

Author: Yatharth Oza (username: yatharth100)

Difficulty: Easy



One string is given as input in each test case containing ‘a’ to ‘z’ and ‘0’ to ‘9’. Your task is to find all alphabet’s ASCII value and from that you need to find highest digit of it (e.g-> for ‘d’ ASCII is ‘100’ so ‘1’ is highest digit from all 3 digits) and take directly that number for ‘0’ to ‘9’. At last you need to print sum of all digits which you have found for all character as output.


For this question we can traverse whole string and abstract their ASCII one by one. Now we have to check if our ASCII is >= 48 and <=57 then we are taking directly 0 to 9 respectively. For all other ASCII value we need to find highest digit from it. We can do this by travelling one by one digit in value. For simplicity we can make one swich case in with we can return directly the highest number.


/*Solution is in JAVA and file name is */
import java.util.*;
    import java.lang.*;
    public class Main {
        public static void main(String args[]) throws IOException{
        BufferedReader br=new BufferedReader(new InputStreamReader(;
        int aa=Integer.parseInt(br.readLine());
        for(int i=0;i<aa;i++){
            String n=br.readLine();
           int ans=0;
           char c[]=n.toCharArray();
           for(int j=0;j<c.length;j++){
               int ret=(add(c[j]));
               int cc=c[j];
        static int add(char c){
            int ret=0;
            int a=c;//Character.getNumericValue(c);
            if(a>=48 && a<=57)a=a-48;
            return ret;