Skip to content

Instantly share code, notes, and snippets.

@2bethere 2bethere/Hill.java Secret
Created Aug 2, 2014

Embed
What would you like to do?
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
You can’t perform that action at this time.