patternjavaMinor
Finding the largest mirror image of a subset/set of integers present in an array of integers
Viewed 0 times
theimagepresentarraymirrorlargestintegersfindingsubsetset
Problem
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.
Conditions for solving:
The solution I got was a little messy, and any cleaner solutions are welcome.
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
Conditions for solving:
- No other helper methods.
- Do not use
Java.util.Arrays.copyOfor any other utility underArrays
- Do not use collections.
The solution I got was a little messy, and any cleaner solutions are welcome.
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=0&&(countmaxCount)?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;
}Solution
Readibility
Whitespaces are free and make things easier to read. Also, you do not need that many parenthesis.
Also, indentation seems a bit weird. After fixing this, here is what I have :
Re-writting the logic
Instead of using
Also, if you have to consider
Then, you can remove common code from the
You can use
You could move your check
Please note that things could be done in an even more efficient way using a different data structure as per the wikipedia page about Longest Common Substring problem.
Whitespaces are free and make things easier to read. Also, you do not need that many parenthesis.
Also, indentation seems a bit weird. After fixing this, here is what I have :
public class MaxMirror {
public static 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= 0 && (countmaxCount)?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;
}
public static void main(String[] args) {
System.out.println("Hello, world!");
int[] num = {1, 2, 3, 8, 9, 3, 2, 1};
System.out.println(maxMirror(num));
int[] num2 = {1, 2, 1, 4};
System.out.println(maxMirror(num2));
int[] num3 = {7, 1, 2, 9, 7, 2, 1};
System.out.println(maxMirror(num3));
}
}Re-writting the logic
Instead of using
continue, you could just use else between your conditions.Also, if you have to consider
A && B and then A && !B, you should probably consider A and then, as a subcase, the validity of B.Then, you can remove common code from the
then block and the else block.You can use
max instead of checking which value is bigger.You could move your check
count
- instead of accessing the
j-1th element from J, we access the n-j`th element from X (simulating a backward traversal).Please note that things could be done in an even more efficient way using a different data structure as per the wikipedia page about Longest Common Substring problem.
Code Snippets
public class MaxMirror {
public static 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;
}
public static void main(String[] args) {
System.out.println("Hello, world!");
int[] num = {1, 2, 3, 8, 9, 3, 2, 1};
System.out.println(maxMirror(num));
int[] num2 = {1, 2, 1, 4};
System.out.println(maxMirror(num2));
int[] num3 = {7, 1, 2, 9, 7, 2, 1};
System.out.println(maxMirror(num3));
}
}public static 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; j--)
{
if (nums[count] == nums[j])
{
if (flag)
{
tempCount++;
maxCount = Math.max(tempCount, maxCount);
}
flag = true;
count++;
if (count>=len)
break;
}
else if (nums[i] != nums[j] && flag)
{
flag = false;
count = i;
tempCount = 1;
}
else if (j == count || j == (count+1))
{
flag = false;
break;
}
}
}
return maxCount;
}public class MirrorString {
/* Returns length of longest common substring of X and Y */
public static int LCSubStr(int[] X /* WAS , int[] Y*/)
{
int m = X.length;
int n = m; // WAS int n = Y.length;
// Create a table to store lengths of longest common suffixes of
// substrings. Notethat LCSuff[i][j] contains length of longest
// common suffix of X and Y. The first row and
// first column entries have no logical meaning, they are used only
// for simplicity of program
int[][] LCSuff = new int[m+1][n+1];
int result = 0; // To store length of the longest common substring
/* Following steps build LCSuff[m+1][n+1] in bottom up fashion. */
for (int i=0; i<=m; i++)
{
for (int j=0; j<=n; j++)
{
if (i == 0 || j == 0)
LCSuff[i][j] = 0;
else if (X[i-1] == X[n-j]) // WAS else if (X[i-1] == Y[j-1])
{
LCSuff[i][j] = LCSuff[i-1][j-1] + 1;
result = Math.max(result, LCSuff[i][j]);
}
else LCSuff[i][j] = 0;
}
}
return result;
}
public static void main(String[] args) {
System.out.println("Hello, world!");
System.out.println(LCSubStr(new int[] {7, 7, 7, 5, 6, 7, 7})); // 3
System.out.println(LCSubStr(new int[] {7, 7, 7, 7, 6, 7, 7})); // 5
System.out.println(LCSubStr(new int[] {})); // 0
System.out.println(LCSubStr(new int[] {1})); // 1
System.out.println(LCSubStr(new int[] {1, 1})); // 2
System.out.println(LCSubStr(new int[] {1, 1, 1})); // 3
System.out.println(LCSubStr(new int[] {1, 2, 3, 2, 1})); // 5
System.out.println(LCSubStr(new int[] {1, 2, 3, 8, 9, 3, 2, 1})); // 3
System.out.println(LCSubStr(new int[] {1, 2, 1, 4})); // 3
System.out.println(LCSubStr(new int[] {7, 1, 2, 9, 7, 2, 1})); // 2
}
}Context
StackExchange Code Review Q#55145, answer score: 8
Revisions (0)
No revisions yet.