Skip to content

Instantly share code, notes, and snippets.

@P7h
Last active April 14, 2016 21:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save P7h/ea58d29de6cd07d7db1d to your computer and use it in GitHub Desktop.
Save P7h/ea58d29de6cd07d7db1d to your computer and use it in GitHub Desktop.
Find the largest of a sub-array for a specific window size
int[] arr = [-1, 3, 1, 5,3 ,2 ,1, 0, 7];
int window = 3
int len = arr.length;
for(int i = 0; i< len-window+1; i += 1) {
int[] newarr = new int[window];
for(int j =0 ; j< window; j++){
newarr[j] = arr[i+j];
}
println(getMax(newarr))
}
String getMax(int[] newarr) {
int largest = 0;
String str = "";
for(int i =0;i<newarr.length;i++) {
str += newarr[i] + " ";
if(newarr[i] > largest) {
largest = newarr[i];
}
}
return str + " => " + largest;
}
/*
-1 3 1 => 3
3 1 5 => 5
1 5 3 => 5
5 3 2 => 5
3 2 1 => 3
2 1 0 => 2
1 0 7 => 7
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment