Solved

maxMirror challenge

Posted on 2016-08-14
10
113 Views
Last Modified: 2016-08-23
Hi,

I am working on below challenge

http://codingbat.com/prob/p196409

Array-3 > maxMirror
prev  |  next  |  chance
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

i have not completely understood the description.

How below is 3

maxMirror([1, 2, 1, 4]) → 3
0
Comment
Question by:gudii9
  • 5
  • 3
  • 2
10 Comments
 
LVL 37

Expert Comment

by:Gerwin Jansen
ID: 41756129
1 2 1 is a 'mirror' that is 3 characters long
0
 
LVL 7

Author Comment

by:gudii9
ID: 41756461
maxMirror([1, 2, 3, 8, 9, 3, 2, 1]) → 3//1,2,3  has mirror image of 3,2,1 with respect to mirror 8,9

maxMirror([7, 1, 2, 9, 7, 2, 1]) → 2// 1,2 has mirror image of 2,1 with respect to mirror 9.7

maxMirror([1, 2, 1, 4]) → 1,2,1 is base image right where is the mirror image and with respect to what mirror?
0
 
LVL 37

Assisted Solution

by:Gerwin Jansen
Gerwin Jansen earned 250 total points
ID: 41756551
Last one has no real mirror. Real description carefully. 121 and 121 are the same from left to right and from right to left.... So length is 3
0
 
LVL 27

Accepted Solution

by:
d-glitch earned 250 total points
ID: 41756552
The center can be outside of the sequence, or inside.
Write the array forward and reverse, and look for the longest common sequence.

FWD = [7, 1, 2, 9, 7, 2, 1]         REV = [1, 2, 7, 9, 2, 1, 7]
Note that there are actually two sequences of length 2.

FWD = [1, 2, 1, 4]                      REV = [4, 1, 2, 1]
Here there is one sequence of length 3.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41760201
not able to get idea on approach to this challenge?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 37

Expert Comment

by:Gerwin Jansen
ID: 41760784
Pseudo code! Write down what you would do to recognise a 'mirror'.

Look at the first example, I would start at the beginning, I find a 1, then also start and the end and walk back to be begin, see if I can find another 1. If not or if it's the same, then it's no mirror. If there is another 1, then at the beginning, increase one step and repeat from the end where you've found the other 1.
0
 
LVL 27

Expert Comment

by:d-glitch
ID: 41760851
You could also turn this into pseudo code:
Write the array forward and reverse, and look for the longest common sequence.
It doesn't say so it in the challenge, but every element in an array will be a mirror sequence of length 1.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41767106
It doesn't say so it in the challenge, but every element in an array will be a mirror sequence of length 1.
i was not clear on above. can you please elaborate on it? How every element in array is mirror sequence of length 1?
0
 
LVL 7

Author Comment

by:gudii9
ID: 41767191
public int maxMirror(int[] nums) {

		int len = nums.length;
		int total = 0;
		int highest = 0;
		for (int x = 0; x < len; x++) {
			total = 0;
			for (int j = len - 1; x + total < len && j > -1; j--) {
				if (nums[x + total] == nums[j]) {
					total++;
				} else {
					if (total > 0) {
						highest = Math.max(total, highest);
						total = 0;
					}
				}
			}
			highest = Math.max(total, highest);
		}
		return highest;
}

Open in new window


above passed all tests
Expected      Run            
maxMirror([1, 2, 3, 8, 9, 3, 2, 1]) → 3      3      OK      
maxMirror([1, 2, 1, 4]) → 3      3      OK      
maxMirror([7, 1, 2, 9, 7, 2, 1]) → 2      2      OK      
maxMirror([21, 22, 9, 8, 7, 6, 23, 24, 6, 7, 8, 9, 25, 7, 8, 9]) → 4      4      OK      
maxMirror([1, 2, 1, 20, 21, 1, 2, 1, 2, 23, 24, 2, 1, 2, 1, 25]) → 4      4      OK      
maxMirror([1, 2, 3, 2, 1]) → 5      5      OK      
maxMirror([1, 2, 3, 3, 8]) → 2      2      OK      
maxMirror([1, 2, 7, 8, 1, 7, 2]) → 2      2      OK      
maxMirror([1, 1, 1]) → 3      3      OK      
maxMirror([1]) → 1      1      OK      
maxMirror([]) → 0      0      OK      
maxMirror([9, 1, 1, 4, 2, 1, 1, 1]) → 3      3      OK      
maxMirror([5, 9, 9, 4, 5, 4, 9, 9, 2]) → 7      7      OK      
maxMirror([5, 9, 9, 6, 5, 4, 9, 9, 2]) → 2      2      OK      
maxMirror([5, 9, 1, 6, 5, 4, 1, 9, 5]) → 3      3      OK      
other tests
OK      

for every i in forward direction checking every j in reverse. if equal increasing the total variable the highests

package com.solution;

public class MaxMirror {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[] ar = { 1, 2, 3, 8, 9, 3, 2, 1 };
		int[] ar1 = { 1, 2, 1, 4};
		//System.out.println("value===>" + maxMirror(ar));
		System.out.println("value===>" + maxMirror(ar1));
	}

	public static int maxMirror(int[] nums) {
		int len = nums.length;
		int total = 0;
		int highest = 0;
		for (int x = 0; x < len; x++) {
			total = 0;
			for (int j = len - 1; x + total < len && j > -1; j--) {
				if (nums[x + total] == nums[j]) {
					total++;
				} else {
					if (total > 0) {
						highest = Math.max(total, highest);
						total = 0;
					}
				}
			}
			highest = Math.max(total, highest);
		}
		return highest;
	}

}

Open in new window


value===>3




i       j     highest
0     3      

0     2      

0     1      
0     0   3

1    3    

1     2      

1     1      

1     0       3



2    3    

2     2      

2     1      

2     0       3





3   3    

3     2      

3     1      

3     0       3
0
 
LVL 7

Author Comment

by:gudii9
ID: 41767195
any improvements or alternate approaches?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

948 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now