Created
January 19, 2021 03:08
-
-
Save thmain/6ddffc8355be11c9f3badd63ffe44bdd to your computer and use it in GitHub Desktop.
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 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