0
class GfG
{
    public static int palinArray(int[] a, int n)
           {
               int k;
               int s=0;
               int m=1;
               int remainder;
               
               
                  for(int i=0;i<n;i++){
                      k=a[i];
                      while(a[i]!=0){
                          remainder=a[i]%10;
                          s=s*10+remainder;
                          a[i]=a[i]/10;
                      }
                      if(k!=s){
                          m=0;
                          break;
                      }
                     
                      else
                        m=1;
                        
                      
                  }
                  
                  return m;
                
           }
}

I tried to take each element of the array and check whether it is palindrome or not. If all the elements in the given array is palindrome then it should return 1 and if it's not then it should return 0.

for example input 5 111 222 333 444 555 expected output 1 My output 0

Thanks for the help.

  • 1
    use a debugger or at least printing out the values of `k` and `s` when setting `m = 0` || I recommend writing a method to determine if ONE number is a palindrome or not. And use that method in a loop to test all numbers – user16320675 Oct 29 '22 at 15:11
  • https://stackoverflow.com/questions/199184/how-do-i-check-if-a-number-is-a-palindrome – chptr-one Oct 29 '22 at 16:29

1 Answers1

0

You can convert each int into String and then check String for being palindrome:

    static boolean isPalyndrome(String value) {
        for (int i = 0; i < value.length() / 2; ++i)
            if (value.charAt(i) != value.charAt(value.length() - 1 - i))
                return false;
                
        return true;
    }
    
    static boolean allPalyndroms(int[] values) {
        for (int i = 0; i < values.length; ++i)
            if (!isPalyndrome((new Integer(values[i])).toString()))
                return false;
                
        return true;
    }

Then if palinArray signature is fixed you can implement it as

public static int palinArray(int[] a, int n) {
  return allPalyndroms(a) ? 1 : 0;
}
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215