The problem I am talking about is this .
Problem statment:
We'll 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.
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
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
The solution I got was a little messy, any cleaner solutions are welcome.
- No other helper methods.
- Do not use Java.util.Arrays.copyOf
Java.util.Arrays.copyOf
or any other utility under ArraysArrays
- Do not use collections.
Below is myThe solution: I got was a little messy, and any cleaner solutions are welcome.
The problem I am talking about is this .
Problem statment:
We'll 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.
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
The solution I got was a little messy, any cleaner solutions are welcome.
- No other helper methods.
- Do not use Java.util.Arrays.copyOf or any other utility under Arrays
- Do not use collections.
Below is my solution:
The problem I am talking about is this:
We'll 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.
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
- No other helper methods.
- Do not use
Java.util.Arrays.copyOf
or any other utility underArrays
- Do not use collections.
The solution I got was a little messy, and any cleaner solutions are welcome.
The problem I am talking about is this.
Problem statment:
We'll 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.
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
The solution I got was a little messy, any cleaner solutions are welcome.
Conditions for solving:
- No other helper methods.
- Do not use Java.util.Arrays.copyOf or any other utility under Arrays
- Do not use collections.
Below is my solution:
public int maxMirror(int[] nums) {
final int len=nums.length;
if(len==0)
return 0;
int maxCount=1;
boolean flag=false;
for(int i=0;i<len;i++)
{
int tempCount=1;
int count=i;
flag=false;
for(int j=len-1;j>=0&&(count<len);j--)
{
if((nums[count]==nums[j])&&!(flag))
{
flag=true;
count++;
continue;
}
if((nums[count]==nums[j])&&(flag))
{
tempCount++;
count++;
maxCount=(tempCount>maxCount)?tempCount:maxCount;
continue;
}
if(!(nums[i]==nums[j])&&(flag))
{
flag=false;
count=i;
tempCount=1;
continue;
}
if((j==count)||(j-count)==1)
{
flag=false;
break;
}
}
}
return maxCount;
}
The problem I am talking about is this.
Problem statment:
We'll 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.
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
The solution I got was a little messy, any cleaner solutions are welcome.
Conditions for solving:
- No other helper methods.
- Do not use Java.util.Arrays.copyOf or any other utility under Arrays
- Do not use collections.
Below is my solution:
public int maxMirror(int[] nums) {
final int len=nums.length;
if(len==0)
return 0;
int maxCount=1;
boolean flag=false;
for(int i=0;i<len;i++)
{
int tempCount=1;
int count=i;
for(int j=len-1;j>=0&&(count<len);j--)
{
if((nums[count]==nums[j])&&!(flag))
{
flag=true;
count++;
continue;
}
if((nums[count]==nums[j])&&(flag))
{
tempCount++;
count++;
maxCount=(tempCount>maxCount)?tempCount:maxCount;
continue;
}
if(!(nums[i]==nums[j])&&(flag))
{
flag=false;
count=i;
tempCount=1;
continue;
}
if((j==count)||(j-count)==1)
{
flag=false;
break;
}
}
}
return maxCount;
}
The problem I am talking about is this.
Problem statment:
We'll 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.
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
The solution I got was a little messy, any cleaner solutions are welcome.
Conditions for solving:
- No other helper methods.
- Do not use Java.util.Arrays.copyOf or any other utility under Arrays
- Do not use collections.
Below is my solution:
public int maxMirror(int[] nums) {
final int len=nums.length;
if(len==0)
return 0;
int maxCount=1;
boolean flag=false;
for(int i=0;i<len;i++)
{
int tempCount=1;
int count=i;
flag=false;
for(int j=len-1;j>=0&&(count<len);j--)
{
if((nums[count]==nums[j])&&!(flag))
{
flag=true;
count++;
continue;
}
if((nums[count]==nums[j])&&(flag))
{
tempCount++;
count++;
maxCount=(tempCount>maxCount)?tempCount:maxCount;
continue;
}
if(!(nums[i]==nums[j])&&(flag))
{
flag=false;
count=i;
tempCount=1;
continue;
}
if((j==count)||(j-count)==1)
{
flag=false;
break;
}
}
}
return maxCount;
}