-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMax mirror problem
44 lines (36 loc) · 1.21 KB
/
Max mirror problem
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
We say that a mirror section in an array is a group of contiguous elements such that somewhere in the array, the same group appears in reverse order. For
example, the largest mirror section in {1, 2, 3, 8, 9, 3, 2, 1} is length 3 (the {1, 2, 3} part). Return the size of the largest mirror section found in the given array.
eg. maxMirror([1, 2, 3, 8, 9, 3, 2, 1]) → 3
maxMirror([1, 2, 1, 4]) → 3
maxMirror([7, 1, 2, 9, 7, 2, 1]) → 2
SOLUTION:
#include<bits/stdc++.h>
using namespace std;
int main() {
int size,maxcount=0,pcount=0;
cin>>size;
int *arr= new int[size];
int x=0,y=0;
for(int i=0;i<size;i++) cin>>arr[i]; //inserting value in the array
for(int i=0; i<size; i++) //checking for repeated pattern
{
pcount=0;
for(int j=(size-1) ; j>i ; j--)
{
if( arr[i]==arr[j] )
{
x=i,y=j;
while( arr[x]==arr[y] )
{
pcount++;
x++;
y--;
}
break;
}
}
//cout<<" for i= "<<i<<" , pcount= "<<pcount<<endl;
if(pcount>maxcount) maxcount=pcount;
}
cout<<maxcount;
}