patternjavaMinor
Codingbat maxMirror challenge
Viewed 0 times
maxmirrorcodingbatchallenge
Problem
I request you to provide insightful suggestions to improve the following code as well as an alternative algorithm to solve it.
Problem Specification:
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.
Solution:
```
public class MaxMirror {
public static void main(String[] args) {
MaxMirror max = new MaxMirror();
int[] nums = { 1, 2, 1, 4 };
System.out.println("The maximum length of the mirror is "
+ max.maxMirror(nums));
}
public int maxMirror(int[] nums) {
if (nums.length == 0) {
return 0;
}
// Reverse order of the array
int[] revNums = new int[nums.length];
for (int numIndex = nums.length - 1, revIndex = 0; numIndex >= 0; numIndex--, revIndex++) {
revNums[revIndex] = nums[numIndex];
}
// Hope the sub array of maximum length is the array itself
int subArraysToBeChecked = 1;
for (int len = nums.length; len > 1; len--) {
if (mirrorOfLengthLenExists(nums, revNums, len,
subArraysToBeChecked)) {
return len;
}
// Increase the number of sub-arrays to be checked
subArraysToBeChecked++;
}
// In the worst case return 1
return 1;
}
// Choose start and end i.e. sub-arrays to be checked
public boolean mirrorOfLengthLenExists(int[] nums, int[] revNums, int len,
int subArraysToBeChecked) {
int start = 0;
int end = len - 1;
for (int i = len; i <= nums.length; i++) {
if (mirrorExis
Problem Specification:
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}) → 3maxMirror({1, 2, 1, 4}) → 3maxMirror({7, 1, 2, 9, 7, 2, 1}) → 2Solution:
```
public class MaxMirror {
public static void main(String[] args) {
MaxMirror max = new MaxMirror();
int[] nums = { 1, 2, 1, 4 };
System.out.println("The maximum length of the mirror is "
+ max.maxMirror(nums));
}
public int maxMirror(int[] nums) {
if (nums.length == 0) {
return 0;
}
// Reverse order of the array
int[] revNums = new int[nums.length];
for (int numIndex = nums.length - 1, revIndex = 0; numIndex >= 0; numIndex--, revIndex++) {
revNums[revIndex] = nums[numIndex];
}
// Hope the sub array of maximum length is the array itself
int subArraysToBeChecked = 1;
for (int len = nums.length; len > 1; len--) {
if (mirrorOfLengthLenExists(nums, revNums, len,
subArraysToBeChecked)) {
return len;
}
// Increase the number of sub-arrays to be checked
subArraysToBeChecked++;
}
// In the worst case return 1
return 1;
}
// Choose start and end i.e. sub-arrays to be checked
public boolean mirrorOfLengthLenExists(int[] nums, int[] revNums, int len,
int subArraysToBeChecked) {
int start = 0;
int end = len - 1;
for (int i = len; i <= nums.length; i++) {
if (mirrorExis
Solution
My approach is to have two indexes. The
Once a match is found, we start counting the max length, with one index moving to the left and one to the right. The while condition makes sure we don't go out of bounds. Once we break out of that loop, we may update the max length.
This is the entire class
i index starts at the left and the j index starts at the right of the array. For a length of 4, we would do these comparisons in this order.(0, 3), (0, 2), (0, 1), then (1, 3), (1, 2), then, (2, 3) This is basically a pairwise comparison. (Note the initialization part of the for loop is empty)int frontIndex = 0;
int endIndex = sequence.length - 1;
for( ; frontIndex frontIndex; i--)
{
//stuff here
}
}Once a match is found, we start counting the max length, with one index moving to the left and one to the right. The while condition makes sure we don't go out of bounds. Once we break out of that loop, we may update the max length.
while(frontIndex + currentLength = 0)
{
if(sequence[frontIndex + currentLength] == sequence[i - currentLength])
{
currentLength++;
}
else break;
}
if (currentLength > maxLength) maxLength = currentLength;
currentLength = 0;This is the entire class
public class Mirrors
{
public static void main(String[] args)
{
int maxLength = 1;
int currentLength = 0;
int[] sequence = {1, 2, 1, 2, 1};
int frontIndex = 0;
int endIndex = sequence.length - 1;
for( ; frontIndex frontIndex; i--)
{
if(sequence[frontIndex] == sequence[i])
{
currentLength++;
while(frontIndex + currentLength = 0)
{
if(sequence[frontIndex + currentLength] == sequence[i - currentLength])
{
currentLength++;
}
else break;
}
if (currentLength > maxLength) maxLength = currentLength;
currentLength = 0;
}
}
}
System.out.print("Max length is " + maxLength);
}
}Code Snippets
int frontIndex = 0;
int endIndex = sequence.length - 1;
for( ; frontIndex < endIndex; frontIndex++)
{
for(int i = endIndex ; i > frontIndex; i--)
{
//stuff here
}
}while(frontIndex + currentLength < sequence.length && i - currentLength >= 0)
{
if(sequence[frontIndex + currentLength] == sequence[i - currentLength])
{
currentLength++;
}
else break;
}
if (currentLength > maxLength) maxLength = currentLength;
currentLength = 0;public class Mirrors
{
public static void main(String[] args)
{
int maxLength = 1;
int currentLength = 0;
int[] sequence = {1, 2, 1, 2, 1};
int frontIndex = 0;
int endIndex = sequence.length - 1;
for( ; frontIndex < endIndex; frontIndex++)
{
for(int i = endIndex; i > frontIndex; i--)
{
if(sequence[frontIndex] == sequence[i])
{
currentLength++;
while(frontIndex + currentLength < sequence.length && i - currentLength >= 0)
{
if(sequence[frontIndex + currentLength] == sequence[i - currentLength])
{
currentLength++;
}
else break;
}
if (currentLength > maxLength) maxLength = currentLength;
currentLength = 0;
}
}
}
System.out.print("Max length is " + maxLength);
}
}Context
StackExchange Code Review Q#39551, answer score: 4
Revisions (0)
No revisions yet.