Array-3 > linearIn
prev | next | chance
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.
linearIn([1, 2, 4, 6], [2, 4]) → true
linearIn([1, 2, 4, 6], [2, 3, 4]) → false
linearIn([1, 2, 4, 4, 6], [2, 4]) → true
public boolean linearIn(int[] outer, int[] inner) {
List o=Arrays.asList(outer);
List i=Arrays.asList(inner);
if(o.contains(i)){
return true;
}
else return false;
}
Expected Run
linearIn([1, 2, 4, 6], [2, 4]) → true false X
linearIn([1, 2, 4, 6], [2, 3, 4]) → false false OK
linearIn([1, 2, 4, 4, 6], [2, 4]) → true false X
linearIn([2, 2, 4, 4, 6, 6], [2, 4]) → true false X
linearIn([2, 2, 2, 2, 2], [2, 2]) → true false X
linearIn([2, 2, 2, 2, 2], [2, 4]) → false false OK
linearIn([2, 2, 2, 2, 4], [2, 4]) → true false X
linearIn([1, 2, 3], [2]) → true false X
linearIn([1, 2, 3], [-1]) → false false OK
linearIn([1, 2, 3], []) → true false X
linearIn([-1, 0, 3, 3, 3, 10, 12], [-1, 0, 3, 12]) → true false X
linearIn([-1, 0, 3, 3, 3, 10, 12], [0, 3, 12, 14]) → false false OK
linearIn([-1, 0, 3, 3, 3, 10, 12], [-1, 10, 11]) → false false OK
other tests
X
Your progress graph for this problem
import java.util.Arrays;
import java.util.List;
public class LinearIn {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] outer = { 1, 2, 4, 6 };
int[] inner = { 2, 4 };
System.out.println("is-->" + linearIn(outer, inner));
}
public static boolean linearIn(int[] outer, int[] inner) {
List o = Arrays.asList(outer);
List i = Arrays.asList(inner);
if (o.containsAll(i)) {
return true;
} else
return false;
}
}
is-->falsepublic boolean linearIn(int[] outer, int[] inner) {
int match=0;
int x=0;
if(inner.length==0){
return true;
}
for (int i = 0; i < outer.length; i++) {
if(outer[i]==inner[x]){
match++;
x++;
}
else if(outer[i]>inner[x]){
return false;
}
else if(outer[i]< inner[x]){
return false;
}
if(match==inner.length){
return true;
}
}
return false;
}
Expected Runtried as above failing some tests. please advise
linearIn([1, 2, 4, 6], [2, 4]) → true false X
linearIn([1, 2, 4, 6], [2, 3, 4]) → false false OK
linearIn([1, 2, 4, 4, 6], [2, 4]) → true false X
linearIn([2, 2, 4, 4, 6, 6], [2, 4]) → true false X
linearIn([2, 2, 2, 2, 2], [2, 2]) → true true OK
linearIn([2, 2, 2, 2, 2], [2, 4]) → false false OK
linearIn([2, 2, 2, 2, 4], [2, 4]) → true false X
linearIn([1, 2, 3], [2]) → true false X
linearIn([1, 2, 3], [-1]) → false false OK
linearIn([1, 2, 3], []) → true true OK
linearIn([-1, 0, 3, 3, 3, 10, 12], [-1, 0, 3, 12]) → true false X
linearIn([-1, 0, 3, 3, 3, 10, 12], [0, 3, 12, 14]) → false false OK
linearIn([-1, 0, 3, 3, 3, 10, 12], [-1, 10, 11]) → false false OK
other tests
X
package com.upcast;
public class LinearIn {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] outer={1,2,4,6};
int[] inner={2,4};
System.out.println("linearIn is===>"+linearIn(outer,inner));
}
public static boolean linearIn(int[] outer, int[] inner) {
int match=0;
int x=0;
if(inner.length==0){
return true;
}
for (int i = 0; i < outer.length; i++) {
if(outer[i]==inner[x]){
match++;
x++;
}
else if(outer[i]>inner[x]){
return false;
}
else if(outer[i]< inner[x]){
return false;
}
if(match==inner.length){
return true;
}
}
return false;
}
}
linearIn is===>false
I would just start with the shortest array ...Also debug statements would help here too.
public boolean linearIn(int[] outer, int[] inner) {
int match=0;
int x=0;
if(inner.length==0){
return true;
}
for (int i = 0; i < outer.length; i++) {
if(outer[i]==inner[x]){
match++;
x++;
}
else if(outer[i]>inner[x]){
return false;
}
/* else if(outer[i]< inner[x]){
return false;
}*/
if(match==inner.length){
return true;
}
}
return false;
}
Expected Run
linearIn([1, 2, 4, 6], [2, 4]) → true true OK
linearIn([1, 2, 4, 6], [2, 3, 4]) → false false OK
linearIn([1, 2, 4, 4, 6], [2, 4]) → true true OK
linearIn([2, 2, 4, 4, 6, 6], [2, 4]) → true true OK
linearIn([2, 2, 2, 2, 2], [2, 2]) → true true OK
linearIn([2, 2, 2, 2, 2], [2, 4]) → false false OK
linearIn([2, 2, 2, 2, 4], [2, 4]) → true true OK
linearIn([1, 2, 3], [2]) → true true OK
linearIn([1, 2, 3], [-1]) → false false OK
linearIn([1, 2, 3], []) → true true OK
linearIn([-1, 0, 3, 3, 3, 10, 12], [-1, 0, 3, 12]) → true true OK
linearIn([-1, 0, 3, 3, 3, 10, 12], [0, 3, 12, 14]) → false false OK
linearIn([-1, 0, 3, 3, 3, 10, 12], [-1, 10, 11]) → false false OK
other tests
OK
package com.upcast;
public class LinearIn {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] outer={1,2,4,6};
int[] inner={2,4};
System.out.println("linearIn is===>"+linearIn(outer,inner));
}
public static boolean linearIn(int[] outer, int[] inner) {
int match=0;
int x=0;
if(inner.length==0){
return true;
}
for (int i = 0; i < outer.length; i++) {
if(outer[i]==inner[x]){
match++;
x++;
}
else if(outer[i]>inner[x]){
return false;
}
/* else if(outer[i]< inner[x]){
return false;
}*/
if(match==inner.length){
return true;
}
}
return false;
}
}
linearIn is===>true
when i comment last else if it passes all tests.Not clear how and why?What was the purpose of
29: /* else if(outer[i]< inner[x]){
30: return false;
31: }*/
Please tell us what you were thinking when you wrote that . public boolean linearIn(int[] outer, int[] inner) {
boolean found = false;
int j = 0;
for(int i = 0; i < inner.length; i++){
for(int k = j; k < outer.length; k++){
if(inner[i] == outer[k]){
j = k;
found = true;
break;
}
}
if(!found)return false;
found = false;
}
return true;
}
I don't know which is better.
public boolean linearIn(int[] outer, int[] inner) {
int match=0;
int x=0;
if(inner.length==0){
return true;
}
for (int i = 0; i < outer.length; i++) {
if(outer[i]==inner[x]){
match++;
x++;
}
/*else if(outer[i]>inner[x]){
return false;
}*/
/* else if(outer[i]< inner[x]){
return false;
}*/
if(match==inner.length){
return true;
}
}
return false;
}
public boolean linearIn(int[] outer, int[] inner) {
int match=0;
int x=0;
if(inner.length==0){
return true;
}
for (int i = 0; i < outer.length; i++) {
if(outer[i]==inner[x]){
match++;
x++;
}
/*else if(outer[i]>inner[x]){
return false;
}*/
/* else if(outer[i]< inner[x]){
return false;
}*/
if(match==inner.length){
return true;
}
}
return false;
}
Expected Run
linearIn([1, 2, 4, 6], [2, 4]) → true true OK
linearIn([1, 2, 4, 6], [2, 3, 4]) → false false OK
linearIn([1, 2, 4, 4, 6], [2, 4]) → true true OK
linearIn([2, 2, 4, 4, 6, 6], [2, 4]) → true true OK
linearIn([2, 2, 2, 2, 2], [2, 2]) → true true OK
linearIn([2, 2, 2, 2, 2], [2, 4]) → false false OK
linearIn([2, 2, 2, 2, 4], [2, 4]) → true true OK
linearIn([1, 2, 3], [2]) → true true OK
linearIn([1, 2, 3], [-1]) → false false OK
linearIn([1, 2, 3], []) → true true OK
linearIn([-1, 0, 3, 3, 3, 10, 12], [-1, 0, 3, 12]) → true true OK
linearIn([-1, 0, 3, 3, 3, 10, 12], [0, 3, 12, 14]) → false false OK
linearIn([-1, 0, 3, 3, 3, 10, 12], [-1, 10, 11]) → false false OK
other tests
OK
package com.upcast;
public class LinearIn {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] outer={1,2,4,6};
int[] inner={2,4};
System.out.println("linearIn is===>"+linearIn(outer,inner));
}
public static boolean linearIn(int[] outer, int[] inner) {
int match=0;
int x=0;
if(inner.length==0){
return true;
}
for (int i = 0; i < outer.length; i++) {
if(outer[i]==inner[x]){
match++;
x++;
}
/* else if(outer[i]>inner[x]){
return false;
}*/
/* else if(outer[i]< inner[x]){
return false;
}*/
if(match==inner.length){
return true;
}
}
return false;
}
}
package com.upcast;
public class LinearIn {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] outer={1,2,4,6};
int[] inner={2,4};
System.out.println("linearIn is===>"+linearIn(outer,inner));
}
public static boolean linearIn(int[] outer, int[] inner) {
int match=0;
int x=0;
if(inner.length==0){
return true;
}
for (int i = 0; i < outer.length; i++) {
if(outer[i]==inner[x]){
match++;
x++;
}
/* else if(outer[i]>inner[x]){
return false;
}*/
else if(outer[i]< inner[x]){
return false;
}
if(match==inner.length){
return true;
}
}
return false;
}
}
We search for when values match.
Why do you care if the values are greater or less than?
public boolean linearIn(int[] outer, int[] inner) {
List o=Arrays.asList(outer);
List i=Arrays.asList(inner);
if(o.contains(i)){
return true;
}
else return false;
}
above collection list approach work?
package com.upcast;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class LinearIn {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] outer={1,2,4,6};
int[] inner={2,3,4};
System.out.println("linearIn is===>"+linearIn(outer,inner));
}
public static boolean linearIn(int[] outer, int[] inner) {
//public boolean linearIn(int[] outer, int[] inner) {
// List o=Arrays.asList(outer);
// List i=Arrays.asList(inner);
Set<Integer> set1 = new HashSet<Integer>(Arrays.asList(outer));
Set<Integer> set2 = new HashSet<Integer>(Arrays.asList(inner));
if(set1.contains(set2)){
return true;
}
else return false;
//}
}
}