Sorting the list of palindromes
sort ( palindromes . begin ( ) , palindromes . end ( ) ) ; int l = 0 , r = palindromes . size ( ) - 1 ; while ( l < r ) { if ( palindromes [ l ] + palindromes [ r ] == N ) return 2 ; else if ( palindromes [ l ] + palindromes [ r ] < N ) ++ l ; else -- r ; }