Finding longest palindrome

Ok so i have this
int a[13]={9,5,3,3,3,6,5,6,4,3,3,4,6}

and i need to find the longest palindrome there could anyone explain and give me the algorithm please?

So what do you have so far? No one here will give you the answer!

I'll give you a starting point.

I find, the best way to find a palindrome is to start in the middle and work your way outwards, testing if each of the indices from the center are the same.

i.e.
index 5 == index 7,
index 4 == index 8,
...
index 0 == index 12


If they all match, then it's a palindrome.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>

int main()
{
	int arrayS[ 11 ] = { 1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1 };
	
	int middle = ( ( sizeof( arrayS ) / sizeof( int ) ) / 2 );

	for( int i = 0; i < middle; ++i )
	{
		//Checks here...
	}

	return 0;
}
Last edited on
I know how to check if it's a palindrome or not i don't know how to see which part of it is the longest palindrome cuz in my array not all the numbers are palindromes.Because the question was to find the longest palindrome in an array which doesn't contain only palindromes
Last edited on
Use nested for-loops. The outer loop will be where you search from and the inner will check for palindrome. If it's palindrome, save it... If it's shorter than the last, don't save it.
Ok i made the inner loop but i don't know how to make the outer one can you give me an example please?
hey you should post what you've got so far and it'd probably be easy to fill in the pieces
Last edited on
1
2
int arrayS[ 11 ] = { 1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1 };
std::cout << ( sizeof( arrayS ) / sizeof( arrayS[ 0 ] ) ) << '\n';


The above code ( sizeof( arrayS ) / sizeof( arrayS[ 0 ] ) will give you the size of the array, which will contain the data to be checked for palindromes. This will be the outer loop.
Thanks for the help guys but i still can't manage to do it i guess it's just above my level as i just learned about palindromes today :\
@ sniperfz

if you post what you have so far, it'd give us a better chance to help
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
int arrayS[13] = {9,5,3,3,3,6,5,6,4,3,3,4,};
std::cout<<(sizeof (arrayS) / sizeof ( arrayS[0] ) ) <<'\n';
for(int i=0;i<5;i++) // i used all the conditions i know and still to no success so the problem is either in my loop or in the outer loop can't figure though.Thanks anyways for wasting your time by trying to help out a noob like me xD
{
oh I just wish I could be more helpful.
for one thing, since you are using std namespace:

using namespace std;
I don't think you need to put "std::" before cout..
it knows whats you means

@ lynx:
what exactly does this bit return? Never run across this before:
sizeof(arrayS[0])

@sniperfz
if the problem is with the loops, post the code that contains them!
hint: you'll want to start the outer loop with an int with a value of 1, and end when that int > (sizeof(array) - 2)
then you can use the int as delimiter to access elements in the array (skipping, of course, the first and last terms, since a palindrome 1 number long doesn't do you any good)
example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int check_for_palindrome(int s);

int main()
{
    int a_nums[13] = {blah blah values here};
    int length=0;
    int longest=0;
    int long_pos=0;
    for (int x=1; x<(sizeof(array) -1); x++)
    {
        length = check_for_palindrome(x) // will access a_nums[x]
        if (length > longest)
        {
            longest = length;
            long_pos = x;
        }
    }
}

there's a little bit of what you need

EDIT this code won't work lol need to change the for loop
I thought sizeof() was like vector.size()
maybe like lynx says
( sizeof( arrayS ) / sizeof( arrayS[ 0 ] )
should be included in conditional statement in for()
Last edited on
It's the size of the element in bytes. My computer shows 4 bytes for an int.
As myArray is of type: int.

Run the following, it may help you under stand it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <sstream>

int main()
{
	int myArray[ 5 ];

	std::cout << "One element holds " << sizeof( myArray[ 0 ] ) << " bytes\n";
	std::cout << "Elements * type: " << sizeof( myArray ) << '\n';

	int total = sizeof( myArray ) / sizeof( myArray[ 0 ] );

	std::cout << "Total elements in the array: " << total << '\n';

	return 0;
}



Sniperfz, I just wrote it quickly, buggy as hell, as it tells you there are palindrome where there aren't any. But it always picks up the palindromes.

Hopefully you can get the idea from it. I would try to fix it, but where would the fun be in that? lol.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
int main()
{
	//int arrayS[ 11 ] = { 1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1 };
	int arrayS[ 10 ] = { 5, 3, 2, 3, 2, 5, 4, 6, 4, 5 };
	
	int middle = ( ( sizeof( arrayS ) / sizeof( arrayS[ 0 ] ) ) / 2 );

	for( int index = 0; index < ( sizeof( arrayS ) / sizeof( arrayS[ 0 ] ) ); ++index )
	{
		std::cout << "Index " << index + 1 << '\n';
	
		for( int i = 0; i < index; ++i )
		{
			if( ( index - ( i + 1 ) ) < 0 )
				break;

			if( ( index + ( i + 1 ) ) > ( sizeof( arrayS ) / sizeof( arrayS[ 0 ] ) ) )
				break;

			if( arrayS[ index - ( i + 1 ) ] == arrayS[ index + ( i + 1 ) ] )
				std::cout << "\tyes\n";
		}
	}
	
	//wait( "Exit of program...\n" );

	return 0;
}


Lines 14 and 17. Don't read off the array. Access Violation.
Line 20: Branch outwards from the current index Checking for palindrome.
ah very cool lynx
u learned me somethin new today
Thanks but now when i try to run the program i get this :
[Linker error] undefined reference to `check_for_palindrome(int)'
ld returned 1 exit status
C:\Dev-Cpp\Makefile.win [Build Error] [Project1.exe] Error 1
[Linker error] undefined reference to `check_for_palindrome(int)'

That's the function you need to create, which will do the checking for a palindrome! Which return( int ) the size of the palindrome.

And no problem, cPlusN00b. (:
Last edited on
Once again guys thank you for the awesome help.

i don't wan to sound like a scumbag but can you make the hints simpler? as i am only in the 10th grade and we didn't use stuff like " (size of (ArrayS) " etc in class and i don't know how they work exactly.So this program looks like rocket science to me compared to what we normally do

You can simply substitute the sizeof( array ) with the length of the array.

1
2
3
4
5
6
7
8
const int maxSize = 5;

int myArray[ maxSize ] = { 1, 2, 3, 4, 5 };

for( int i = 0; i < maxSize; ++i )
{
	//code...
}


I only use sizeof if I don't know the size of the array etc.
Then if I ever change the size of the array, I don't need to change any other code. It makes it more dynamic for changing. (:
Last edited on
Topic archived. No new replies allowed.