blob: 6d7d2099ef68f764af26bf846fff7a062a863154 (
plain) (
blame)
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
#include <iostream>
using namespace std;
const int SIZE = 30;
bool isSubarrayPalindrome(int arr[SIZE], int startIndex, int endIndex)
{
while (startIndex < endIndex)
{
if (arr[startIndex] != arr[endIndex])
return false;
startIndex++;
endIndex--;
}
return true;
}
void print(int arr[SIZE])
{
for (int i = 0; i < SIZE; i++)
cout << arr[i];
cout << endl;
}
int LongestPalindromicSubarray(int arr[SIZE], int n)
{
int currentSize = n;
int startIndex = 0;
int endIndex = startIndex + currentSize-1;
while (true)
{
if (isSubarrayPalindrome(arr, startIndex, endIndex))
return endIndex - startIndex + 1;
startIndex++;
endIndex++;
if (endIndex == n)//last iteration
{
currentSize--;
startIndex = 0;
endIndex = startIndex + currentSize - 1;
}
}
}
int main()
{
int arr[SIZE] = {1,4,3,2,3};
cout << LongestPalindromicSubarray(arr, 5);
}
|