Last active
June 24, 2023 12:42
-
-
Save deepaksinghvi/cf5b19e19c5c98b84db37b2dcd006c3c to your computer and use it in GitHub Desktop.
LargestGap
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
import java.util.Arrays; | |
public class LargestGap { | |
public static void main(String[] args) { | |
System.out.println(largestGap(new int[]{9, 4, 26, 26, 0, 0, 5, 20, 6, 25, 5})); | |
} | |
public static int largestGap(int[] numbers) { | |
int result = 0; | |
Arrays.sort(numbers); | |
for(int i=0;i<numbers.length-1;i++) { | |
int diff = numbers[i+1]- numbers[i]; | |
if (result < diff) { | |
result = diff; | |
} | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment