-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPalindromArray.cpp
More file actions
67 lines (55 loc) · 1.27 KB
/
Copy pathPalindromArray.cpp
File metadata and controls
67 lines (55 loc) · 1.27 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
Is this given Array a Palindrome?
Problem Statement
Write a function palindrome that takes an integer
array and returns true if the array is a palindrome
else it returns false.
Lets solve this problem from different approaches.
*/
#define FORREF
#ifndef FORREF
using namespace std;
#include<iostream>
#include<string>
struct Palindrome {
// Iterative Approach
bool isPalindrom_iter(int array[], int left, int right) {
while (left < right) {
if (array[left] != array[right]) {
return false;
}
++left;
--right;
}
return true;
}
// Recursive Approach
bool isPalindrom(int array[], int left, int right) {
bool res = false;
if (left >= right) {
return true;
}
else
{
if (array[left] != array[right]) {
return false;
}
return isPalindrom(array, left + 1, right - 1);
}
}
};
// Driver code
int main()
{
int array[] = { 1, 2, 2, 2, 1 };//declare an array
int n = sizeof(array) / sizeof(array[0]);//calculate the size of the array
int startIndex = 0;//define the starting index
int endIndex = n - 1;//define the ending index
Palindrome pa;
if (pa.isPalindrom(array, startIndex, endIndex) == 1) //call recursive function
cout << "Array is a Palindrome";
else
cout << "Array is not Palindrome";
return 0;
}
#endif // FORREF