Palindrome issue

I am required to make a program that finds if a vector is a palindrome in as few lines as possible. It uses this base program.

#include <iostream>
using namespace std;
int main(void) {
int vector[] = {1, 7, 3, 8, 3, 7, 1};
bool ispalindrome = true;
int n = sizeof(vector) / sizeof(vector[0]);
// Insert your code here
if(ispalindrome)
cout << "It's a palindrome";
else
cout << "It isn't a palindrome";
cout << endl;)
return 0;

(it is okay for it to be hardcoded but I need as little lines as possible.
Last edited on
When you say "a vector", do you mean a vector or an array? They're very different things.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <vector>
using namespace std;

bool isPalindrome( vector<int> &V, int L, int R )
{
   return L >= R || ( V[L] == V[R] && isPalindrome( V, L + 1, R - 1) );
}

int main()
{
   vector<int> U = { 1, 3, 5, 4, 5, 3, 1 };   cout << boolalpha << isPalindrome( U, 0, U.size() - 1 ) << '\n';
   vector<int> V = { 1, 3, 5, 4, 3, 5, 1 };   cout << boolalpha << isPalindrome( V, 0, V.size() - 1 ) << '\n';
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

int main() {

    const int vector[] = { 1, 7, 3, 8, 3, 7, 1 };
    const int n = sizeof(vector) / sizeof( vector[0] );

    bool is_palindrome = true ;

    // in as few lines as possible.
    for( int i = 0 ; i < n/2 ; ++i ) if( vector[i] != vector[n-i-1] ) is_palindrome = false ;

    std::cout << ( is_palindrome ? "It's" : "It isn't" ) << " a palindrome\n" ;
}
Topic archived. No new replies allowed.