Skip to main content
Code Review

Return to Question

deleted 42 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

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.

  1. No other helper methods.
  2. Do not use Java.util.Arrays.copyOfJava.util.Arrays.copyOf or any other utility under ArraysArrays
  3. 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.

  1. No other helper methods.
  2. Do not use Java.util.Arrays.copyOf or any other utility under Arrays
  3. 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
  1. No other helper methods.
  2. Do not use Java.util.Arrays.copyOf or any other utility under Arrays
  3. Do not use collections.

The solution I got was a little messy, and any cleaner solutions are welcome.

added 13 characters in body
Source Link
Anirudh
  • 872
  • 2
  • 13
  • 28

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:

  1. No other helper methods.
  2. Do not use Java.util.Arrays.copyOf or any other utility under Arrays
  3. 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:

  1. No other helper methods.
  2. Do not use Java.util.Arrays.copyOf or any other utility under Arrays
  3. 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:

  1. No other helper methods.
  2. Do not use Java.util.Arrays.copyOf or any other utility under Arrays
  3. 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; 
 
}
edited title
Link
Anirudh
  • 872
  • 2
  • 13
  • 28

Finding the largest mirror image of a subset/set of integers present in an array of integers

edited tags
Link
200_success
  • 145.5k
  • 22
  • 190
  • 479
Loading
Tweeted twitter.com/#!/StackCodeReview/status/481478920181936128
added 25 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238
Loading
added tag
Link
RubberDuck
  • 31.1k
  • 6
  • 73
  • 176
Loading
edited title
Link
Anirudh
  • 872
  • 2
  • 13
  • 28
Loading
Source Link
Anirudh
  • 872
  • 2
  • 13
  • 28
Loading
lang-java

AltStyle によって変換されたページ (->オリジナル) /