Question: ```
package testing.project;

public class PalindromeThreeDigits {

    public static void main(String[] args) {
        int value = 0;
        for(int i = 100;i <=999;i++)
        {
            for(int j = i;j <=999;j++)
            {
                int value1 = i * j;
                StringBuilder sb1 = new StringBuilder(""+value1);
                String sb2 = ""+value1;
                sb1.reverse();
                if(sb2.equals(sb1.toString()) && value<value1) {
                    value = value1;

                }

            }
        }

        System.out.println(value);
    }
}

```

This is the code that I wrote in Java... Is there any efficient way other than this.. And can we optimize this code more??

Answer: 
A slightly different approach that can easily calculate the largest palindromic number made from the product of up to two 6-digit numbers. 

The first part is to create a generator of palindrome numbers. So there is no need to check if a number is palindromic, the second part is a simple loop.

```
#include <memory>
#include <iostream>
#include <cmath>

using namespace std;
template <int N>
class PalindromeGenerator {
    unique_ptr <int []> m_data;
    bool m_hasnext;
public :
    PalindromeGenerator():m_data(new int[N])
    {
        for(auto i=0;i<N;i++)
        m_data[i]=9;
        m_hasnext=true;
   }
  bool hasNext() const {return m_hasnext;}

long long int getnext()
{
    long long int v=0;
    long long int b=1;
    for(int i=0;i<N;i++){
        v+=m_data[i]*b;
        b*=10;
    }
    for(int i=N-1;i>=0;i--){
        v+=m_data[i]*b;
        b*=10;
    }

    auto i=N-1;
    while (i>=0)
    {
        if(m_data[i]>=1) {
            m_data[i]--;
            return v;
        }
        else 
        {
            m_data[i]=9;
           i--; 
        }
    }

    m_hasnext=false;
    return v;
}

};

template<int N>
void findmaxPalindrome()
{
    PalindromeGenerator<N> gen;
    decltype(gen.getnext()) minv=static_cast<decltype(gen.getnext())> (pow(10,N-1));
    decltype(gen.getnext()) maxv=static_cast<decltype(gen.getnext())> (pow(10,N)-1);
    decltype(gen.getnext()) start=11*(maxv/11);
    while(gen.hasNext())
    {
        auto v=gen.getnext();
        for (decltype(gen.getnext())  i=start;i>minv;i-=11)
        {
            if (v%i==0)
            {
                auto r=v/i;
                if (r>minv && r<maxv ){
                    cout<<"done:"<<v<<" "<<i<< "," <<r <<endl;
                    return ;
                }
            }

      }
    }

return ;
}
int main(int argc, char* argv[])
{
    findmaxPalindrome<6>();
    return 0;
}

```
```
i did this my way , but m not sure if this is the most efficient way of doing this .

package problems;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

  public class P_4 {

/**
 * @param args
 * @throws IOException 
 */
static int[] arry = new int[6];
static int[] arry2 = new int[6];

public static boolean chk()
{
    for(int a=0;a<arry.length;a++)
        if(arry[a]!=arry2[a])
            return false;

return true;

}

public static void main(String[] args) throws IOException {
    // TODO Auto-generated method stub

    InputStreamReader ir = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader(ir);
    int temp,z,i;

for(int x=999;x>100;x--)
    for(int y=999;y>100;y--)
        {
        i=0;
            z=x*y;
            while(z>0)
                {
                    temp=z%10;
                    z=z/10;
                    arry[i]=temp;
                    i++;
                }

            for(int k = arry.length;k>0;k--)
                        arry2[arry.length- k]=arry[k-1];

    if(chk())
    {
        System.out.print("pelindrome = ");

for(int l=0;l<arry2.length;l++)
System.out.print(arry2[l]);
System.out.println(x);
System.out.println(y);
}

    }
}
}

```