-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind_palindromic_subarray_sw.cpp
61 lines (47 loc) · 1.06 KB
/
find_palindromic_subarray_sw.cpp
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
// subarray where concatinating all the elements form a palindrome using sliding window(sw)
#include<bits/stdc++.h>
using namespace std;
bool isPalindrome(int num) {
int temp = num, n = 0;
while (temp > 0)
{
n = n*10 + temp%10;
temp /= 10;
}
if(num == n) {
return true;
}
return false;
}
int findPalindromicSubarr(vector<int> arr, int k) {
int num = 0;
for(int i=0; i<k; i++) {
num = num*10 + arr[i];
}
if(isPalindrome(num)) {
return 0;
}
for(int i=k; i<arr.size(); i++) {
num = (num% (int)pow(10, k-1)) * 10 + arr[i];
if(isPalindrome(num)) {
return i-k+1;
}
}
return -1;
}
int main() {
vector<int> arr = {2, 3, 5, 1, 1 ,5};
int k = 4;
int ans = findPalindromicSubarr(arr, k);
if(ans == -1) {
cout<<"Palindromic subarray doesn't exist in this array"<<endl;
}
else
{
for(int i=ans; i<ans+k; i++) {
cout<<arr[i]<<" ";
}
cout<<endl;
}
return 0;
}