Can anyone explain me what is wrong with this code?

/* package codechef; // don’t place package namimport java.util.;
import java.lang.
;
import java.io.*;

/* Name of the class has to be “Main” only if the class is public. */
class codechef
{
public static void main(String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int testcase=0;
String output = “”;
testcase = Integer.parseInt(sc.next());
//sc.nextLine();
while(testcase–>0){

	    String str = "";
        int first=0,second=0;
	    str = sc.next();
	 

	    for(int i=0;i<str.length();i++)
	    {
            if(str.length()%2==0)
            {
                if(i<str.length()/2){
                   
                first+=(int)str.charAt(i);
                }
                else if(i>=str.length()/2)
                {
                  
                second+=(int)str.charAt(i);

                }
            }
            else
            {
                if(i<str.length()/2){
                    
                first+=(int)str.charAt(i);
                }
                else if(i>=str.length()/2+1)
                {
                   
                second+=(int)str.charAt(i);

                }
            }
	        
	        
        }     
       
	       if(first==second)
	       {
	           output+="YES ";
	          // System.out.println("YES");
	       }
	       else{
	           output+="NO ";
	           //System.out.println("NO ");
	       }
	    
        
          
           
	}
	String arr[] = output.split(" ");
	for(String a: arr)
	System.out.println(a);
   
	
}

}"’

Please either format your code or (better!) link to your submission - the forum software has mangled it and it won’t compile! :slight_smile:

Also - what Problem are you trying to solve? :slight_smile:

2 Likes

I’m trying to solve lapindrome problem, divide the string in two equal parts,if string length is odd ignore middle character and Check frequency of both the parts of it if frequency is equal print “YES” if not equal print “NO”

And it works on custom test cases but giving wrong answer.
And aslo after running it throwing runtime error but works on offline ide
Input :
4
gaga
abcde
xyzxy
bbb

Output:
YES
NO
YES
YES

Hi dear,
although I am not much knowledgeable with java, and believing that string.charAt(x) will give you character at x index of the string.
Consider the case: abba
this isn’t a lapindrome but when you will find first and second with your logic. I will result in same value.

(ps: try making first and second strings)

Thank you for your kind reply
But “abba” is a lapindrome
What my will do is it will take int values of ‘a’ and ‘b’ and add them and store it in first
And for remaining half string store addition of int values of ‘b’ and ‘a’ and store it in second

And it will compare first and second if they are equal it will print yes and if not equal print no.