Skip to content

Instantly share code, notes, and snippets.

@ar852
Created July 15, 2021 20:15
Show Gist options
  • Save ar852/9ac6b240e9563ba40c199b58bf9c2822 to your computer and use it in GitHub Desktop.
Save ar852/9ac6b240e9563ba40c199b58bf9c2822 to your computer and use it in GitHub Desktop.
Solution for linearIn problem in the Array-3 java section on codingbat.com (uses only one array pass)
public boolean linearIn(int[] outer, int[] inner) {
int i=0, j=0, out, in;
while (i<outer.length && j < inner.length){
out = outer[i]; in = inner[j];
if (out<in){
i++;
}
else if (out==in){
i++;
j++;
while (i<outer.length-1 && outer[i]==in){ // skip past repeating numbers
i++;
}
}
else { // i>o so no matches
return false;
}
}
if (j != inner.length){ // not all numbers in inner were matched
return false;
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment