Simjeff wrote: |
---|
Hello im having a little problem. I need to make a program in which i enter a number between 3 and 1000 and make an array with that many elements in it . Then i need to see how many palindromes are there in that array. I get that you need a for or while and count++ for every palindrome but i dont know how to structure it.
For example if you write down 9 and then the 9 elements are 7 7 7 9 8 9 7 7 7 it should cout 3. ( 777 is a palindrome , 989 is a palindrome and 777989777 is a palindrome.) |
No, the answer would not be 3.
77
79897
7798977
are all also palindromes. Also, 777 appears twice, so should register as 2 palindromes. 77 appears 4 times, so should register as 4 palindromes
The algorithm that occurs to me off the top of my head is:
- Iterate over every element of the array
- For each iteration, iterate from that element to the nearest end of the array, so that you're iterating over every possible length for which the element might be the centre of a palindrome.
- For each inner iteration, determine whether the element is the centre of a palindrome of that length.
(Edited to clarify the structure, because my formatting didn't work.)
Note that the last part will work differently depending on whether you're looking at an even-length palindrome, or an odd-length one.