Skip to content

Instantly share code, notes, and snippets.

@thmain
Created January 19, 2021 03:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thmain/6ddffc8355be11c9f3badd63ffe44bdd to your computer and use it in GitHub Desktop.
Save thmain/6ddffc8355be11c9f3badd63ffe44bdd to your computer and use it in GitHub Desktop.
import java.util.Arrays;
public class isArithmeticProgression {
public static boolean checkArithmeticProgression(int[] arr) {
Arrays.sort(arr);
Integer d = null;
int prev = arr[0];
for(int i=1;i<arr.length;i++){
if(d==null){
d = arr[i]-prev;
}
else if((arr[i]-prev)!=d){
return false;
}
prev = arr[i];
}
return true;
}
public static void main(String[] args) {
System.out.println(checkArithmeticProgression(new int []{3, 5, 1, 7}));
System.out.println(checkArithmeticProgression(new int []{4, 1, 0, 5}));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment