Skip to content

Instantly share code, notes, and snippets.

@rezaiyan
Created July 4, 2020 20:37
Show Gist options
  • Save rezaiyan/95fcf491fbf23b8e50296e6eca5c48dc to your computer and use it in GitHub Desktop.
Save rezaiyan/95fcf491fbf23b8e50296e6eca5c48dc to your computer and use it in GitHub Desktop.
Array degree algo
public static String codeHere(String inputData) {
// Use this function to write your solution;
String[] arrayInfo = inputData.split("\n");
int size = Integer.parseInt(arrayInfo[0]);
int[] lenghsArray = new int[size];
String[] stringArray = arrayInfo[1].split(" ");
List<Integer> subArray = new ArrayList<>();
for (int i = 0; i < size; i++) {
int currentNumber = Integer.parseInt(stringArray[i]);
int nextNumber = -1;
if (i < size - 1)
nextNumber = Integer.parseInt(stringArray[i + 1]);
if (currentNumber == nextNumber) {
subArray.add(currentNumber);
} else {
if (!subArray.isEmpty() || currentNumber == -1)
subArray.add(currentNumber);
lenghsArray[i] = subArray.size();
subArray.clear();
}
}
int minLength = Integer.MAX_VALUE;
for (int i = 0; i < size; i++) {
if (lenghsArray[i] < minLength && lenghsArray[i] > 0)
minLength = lenghsArray[i];
}
return String.valueOf(minLength);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment