Why I am getting wrong answer in TOTR?

MY Solution Link,i Used Simple Approach,Please help me guys…Thanks in advance …My Solution link–https://www.codechef.com/viewsolution/11190003


class tourist
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();

    String enter=sc.next();
    String enter2=enter.toUpperCase();
    String apha="abcdefghijklmnopqrstuvwxyz";
    String apcap="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    while(t!=0)
    {
      
        char ch;
        int index=0;
    String s=sc.next();
    String check;
   String sfinal=s;
     char arr[]=new char[sfinal.length()];
    for(int i=0;i<sfinal.length();i++)
    {
        ch=sfinal.charAt(i);
       if(ch>='A' && ch<='Z')
       {
           check=apcap;  
           index=check.indexOf(ch);
           arr[i]=enter2.charAt(index);
       }
       else if(ch>='a' && ch<='z') 
       {
        check=apha;
        index=check.indexOf(ch);
        arr[i]=enter.charAt(index);
       }
         
      else 
        {
           if(ch=='_')
          {
             arr[i]='$';
          }
           else if(ch=='.')
          {
            arr[i]='*';
          }
           else if(ch==',')
          {
           arr[i]='@';
           }
           else if(ch=='!')
          {
            arr[i]='#';
          }
            else if(ch=='?')
           {
             arr[i]=':';        
            }
        }
  }
    for(int i=0;i<sfinal.length();i++)
  
    {
        if(arr[i]=='$')
        {
              System.out.print(" ");
        }
      else if(arr[i]=='*')
        {
            System.out.print(".");
        }
         else if(arr[i]=='@')
        {
            System.out.print(",");
        }
         else if(arr[i]=='#')
        {
            System.out.print("!");
        } 
         else if(arr[i]==':')
        {
            System.out.print("?");
        } 
         else
        System.out.print(arr[i]);
    }
        System.out.println(" ");       
     t--;
    }
}

}

Your code seems fine except the line “System.out.println(” “);” you are printing a “space” and a new line here.
Just remove the space and try. :slight_smile:

3 Likes

PLEASE HELP ME IN THIS ALSO…
Problem link->https://www.codechef.com/problems/BUYING2
My Solution—…

class buyingsweet

{
public static void main(String[] args)

 {
     Scanner sc=new Scanner(System.in);
    int sum;
     int t=sc.nextInt();
     int count;
    int res;
    boolean done;
     while(t!=0)
     {
         boolean flagcheck=false;
         done=false;
        count=0;
        res=0;
        sum=0;
     int nonotes=sc.nextInt(),sprice=sc.nextInt();
     int s[]=new int[nonotes];
     for(int i=0;i<nonotes;i++)
     {
         s[i]=sc.nextInt();
         sum=sum+s[i];
     }
     if(nonotes==1 && sum>sprice)
     {
         System.out.println("1");
     }
        else if(nonotes>1)
     {
       if(sum%sprice==0)
       {
             System.out.println(sum/sprice);    
             done=true;
       } 
       else if(sum%sprice!=0)
       {
            // count=(sum/sprice);
          Arrays.sort(s);
          for(int i=0;i<nonotes;i++)
          {      
             res=0;
              res=sum-s[i];
              if(res%sprice==0)
              {
                  System.out.println("-1");
                  break;
              }   
              else
              {
                 while(res%sprice!=0)
                {
                    
                   res--;
                   if(res%sprice==0)
                   {    
                      flagcheck=true;  
                      break;
                   }
               
                 }
              }
              if(flagcheck==true)
                  break;
                  }
       }
          if(flagcheck)
          {
                   System.out.println("-1");  
          }
       }
     
     
      if((flagcheck=!true && done==false))   
      System.out.println(count); 
     t--;
     }
 }

}

oh shit!,thanks a lot…
Felling shamed by this type of silly mistake…

If it solved your doubt then please accept this solution. Such mistakes happens with everybody, it is practice which will help you debug such errors.

1 Like

It’s not good to ask new questions in comments. Also you should try to debug your code yourself, it would be very helpful in long run. Still if you need help post it as a new question.

okay i will