Hello I am having a hard time with this assignment. Can someone please let me know why my test number 5 is still saying true when its suppose to be false. Thanks.
This is the question.
Arrays – has12 & linearIn
Given an array of ints, return true if there is a 1 in the array with a 2 somewhere later in the array.
Given two arrays of ints sorted in increasing order, outer and inner, return true if all of the numbers in inner appear in outer. The best solution makes only a single "linear" pass of both arrays, taking advantage of the fact that both arrays are already in sorted order.
This is my output/problem
Test 1: true
Test 2: true
Test 3: false
Test 4: true
Test 5: true//needs to be false
Test 6: true
This is my code
public class Problems {
public static void main(String[] args) {
int[] a = {1,3,2};
int[] b = {3,1,2};
int[] c = {3,1,4,5};
int[] d = {1,2,4,6};
int[] e = {1,2,4,4,6};
int[] f = {2,4};
int[] g = {2,3,4};
boolean test1 = has12(a);
boolean test2 = has12(b);
boolean test3 = has12(c);
System.out.println("Test 1: " + test1); //should print true
System.out.println("Test 2: " + test2); //should print true
System.out.println("Test 3: " + test3); //should print false
System.out.println("Test 4: " + linearIn(d, f)); //should print true
System.out.println("Test 5: " + linearIn(d, g)); //should print false
System.out.println("Test 6: " + linearIn(e, f)); //should print true
}
//has12 method goes here
public static boolean has12(int[] array) {
int i;
int x;
for(x=0;x<array.length;x++){
if(array[x]==1){
}
for(i=x+1;i<array.length;i++){
if (array[i]==2) return true;
}
}
return false;
}
//linearIn method goes here
public static boolean linearIn(int[] outer, int[] inner) {
int i;
int j;
for (i = 0; i < inner.length; i++) {
for (j =0; j < outer.length; j++) {
if (outer[j] == inner[i]) return true;
}
}
return false;
}
}