Created
July 15, 2021 20:15
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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