Skip to content

Commit

Permalink
BMC Interview Question
Browse files Browse the repository at this point in the history
  • Loading branch information
The-Pro-Programmer committed Aug 5, 2021
1 parent 694a041 commit 09aa283
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions GeeksForGeeks/src/interviewQuestion/bmc/NearestNumbers.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package interviewQuestion.bmc;

import java.util.Arrays;

public class NearestNumbers {

static int distClosestNumbers(int[] numbers) {

Arrays.sort(numbers);
int min = Integer.MAX_VALUE;
for(int i=0; i<numbers.length-1; i++){
int diff = Math.abs(numbers[i] - numbers[i+1]);
if(diff<min){
min = diff;
}
}
return min;

}
public static void main(String[] args) {
int[] testArray = {3, 9, 50, 15, 99, 7, 98, 65};
int result = distClosestNumbers(testArray);
System.out.println(result);
}

}

0 comments on commit 09aa283

Please sign in to comment.