-
-
Save 2bethere/680aebe7a9da8744b838 to your computer and use it in GitHub Desktop.
Hill
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
package com.hired; | |
public class Hill { | |
public static int compute(int[] input) | |
{ | |
int upperRange = 0; | |
int i=1; | |
int[] workingArray = input.clone(); | |
int c; | |
boolean valid; | |
while( i < input.length) | |
{ | |
// Making head as low as possible | |
workingArray[0]=input[0]-upperRange; | |
// Start from element 1 | |
workingArray[i] = input[i] - upperRange; | |
// | |
c= -upperRange; | |
valid = true; | |
while(workingArray[i]<=workingArray[i-1]) | |
{ | |
if(c>upperRange){ | |
upperRange++; | |
// Ran out of numbers, let's go back to 0 and retry; | |
valid = false; | |
break; | |
} | |
workingArray[i] = input[i] + c; | |
c++; | |
} | |
if(valid){ | |
i++; | |
}else{ | |
i=1; | |
} | |
} | |
return upperRange; | |
} | |
public static void main(String[] args) { | |
int[] a= {5,4,3,2,8}; | |
System.out.print(Hill.compute(a)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment