Skip to content

Instantly share code, notes, and snippets.

@2bethere
Created August 2, 2014 07:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save 2bethere/680aebe7a9da8744b838 to your computer and use it in GitHub Desktop.
Save 2bethere/680aebe7a9da8744b838 to your computer and use it in GitHub Desktop.
Hill
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