-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
694a041
commit 09aa283
Showing
1 changed file
with
26 additions
and
0 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
GeeksForGeeks/src/interviewQuestion/bmc/NearestNumbers.java
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
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); | ||
} | ||
|
||
} |