Thursday, 16 March 2017

largestsequencegap

package com.paper;

import java.util.Arrays;

public class largestsequencegap {
public static void main(String[] args){
largestsequencegap test = new largestsequencegap();

int[] A = {10, 0, 8, 2, -1, 12, 11, 3};
System.out.println(test.solution(A));

int[] B = {5,5};
System.out.println(test.solution(B));
}


//A - bikes already parked
//A[K] - position of the rack (0 =< K =< N)

//A[K] = 0 , 0 is center rack
//A[k] = +N, N meters right of center
//A[K] = -N, N meters left of center
public int solution (int[] A){

Arrays.sort(A);

int maxgap = 0;
for (int i = 0, j= 1; i < A.length && j < A.length; i++, j++){
int left = A[i];
int right = A[j];
int gap = Math.abs(left - right);
if (gap > maxgap){
maxgap = gap;
}
}
return maxgap;
}
}

No comments:

Post a Comment