Important Notice:

Practice-It will be discontinued as of November 1st, 2024. After this date, the website will remain online for a transitional period, but login will be restricted to University of Washington NetID authentication. This marks the next phase towards the platform's full retirement. Thank you for your use and support of the application over the years.

If you are looking for an alternative, a similar tool, CodeStepByStep, was developed independently by the original author of Practice-It, and is available at codestepbystep.com**

logo Practice-It logo

hasMirrorTwice

Language/Type: Java arrays nested loops
Author: Roy McElmurry

Write a static method named hasMirrorTwice that accepts two arrays of integers a1 and a2 as parameters and returns true if a1 contains all the elements of a2 in reverse order at least twice (and false otherwise). For example, if a2 stores the elements {1, 2, 3} and a1 stores the elements {6, 3, 2, 1, 4, 1, 3, 2, 1, 5}, your method would return true.

Assume that both arrays passed to your method will have a length of at least 1. This means that the shortest possible mirror will be of length 1, representing a single element (which is its own mirror). A sequence that is a palindrome (the same forwards as backwards) is considered its own mirror and should be included in your computations. For example, if a1 is {6, 1, 2, 1, 4, 1, 2, 1, 5} and a2 is {1, 2, 1}, your method should return true. The two occurrences of the mirror might overlap, as shown in the fourth sample call below.

The following table shows some calls to your method and their expected results:

Arrays Returned Value
int[] a1 = {6, 1, 2, 1, 3, 1, 3, 2, 1, 5};
int[] a2 = {1, 2};
hasMirrorTwice(a1, a2) returns true
int[] a3 = {5, 8, 4, 18, 5, 42, 4, 8, 5, 5};
int[] a4 = {4, 8, 5};
hasMirrorTwice(a3, a4) returns false
int[] a5 = {6, 3, 42, 18, 12, 5, 3, 42, 3, 42};
int[] a6 = {42, 3};
hasMirrorTwice(a5, a6) returns true
int[] a7 = {6, 1, 2, 4, 2, 1, 2, 4, 2, 1, 5};
int[] a8 = {1, 2, 4, 2, 1};
hasMirrorTwice(a7, a8) returns true
int[] a9 = {0, 0};
int[] aa = {0};
hasMirrorTwice(a9, aa) returns true
int[] ab = {8, 9, 2, 1};
int[] ac = {5, 7, 1, 2, 9, 8};
hasMirrorTwice(ab, ac) returns false

Do not modify the contents of the arrays passed to your method as parameters.

Type your solution here:


This is a method problem. Write a Java method as described. Do not write a complete program or class; just the method(s) above.

You must log in before you can solve this problem.


Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.