Created
December 16, 2014 00:16
-
-
Save pabloDon/3e6ffa2238e73b4bf30d to your computer and use it in GitHub Desktop.
The App Bussiness Java Test
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
if (inputSequence.size() < 2) { //inputSequence cannot be null | |
return inputSequence.size(); | |
} else { | |
Integer maxSlice = 1; | |
Integer tempSlice = 1; | |
Integer numberOne = inputSequence.get(0); | |
Integer indexOne = 0; | |
Integer numberTwo = null; | |
Integer indexTwo = null; | |
boolean lastCoincidenceWasFirstNumber = true; | |
for (int i = 1; i < inputSequence.size(); i++) { | |
Integer num = inputSequence.get(i); | |
if (numberOne == num || numberTwo == num) { | |
tempSlice ++; | |
//If we have changed the number, we have to update boolean variable and point to that number. | |
if (lastCoincidenceWasFirstNumber && numberTwo == num) { | |
indexTwo = i; | |
} else if (!lastCoincidenceWasFirstNumber && numberOne == num) { | |
indexOne = i; | |
} | |
lastCoincidenceWasFirstNumber = numberOne == num; | |
} else if (numberTwo == null) { | |
//We have found only one coincidence at the moment. Refresh variables. | |
tempSlice ++; | |
numberTwo = num; | |
indexTwo = i; | |
lastCoincidenceWasFirstNumber = false; | |
} else { //Number is different for both. Refresh variables | |
//Refresh largest slice found | |
maxSlice = tempSlice > maxSlice ? tempSlice : maxSlice; | |
//Refresh temp values and return index "i" to the last number of the slice (numberTwo variable) | |
tempSlice = 1; //Restart to 1, because you have one coincidence at least (inputSequence[i]) | |
//Shift pointer for first number in the next "sub-array" | |
i = lastCoincidenceWasFirstNumber ? indexOne : indexTwo; | |
//Shift index two to one | |
if (!lastCoincidenceWasFirstNumber) { | |
//If the first number was not the last coincidence, we shift second number values to first attributes. | |
indexOne = indexTwo; | |
numberOne = numberTwo; | |
} | |
indexTwo = null; | |
numberTwo = null; | |
} | |
} | |
return tempSlice > maxSlice ? tempSlice : maxSlice; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment