https://www.nowcoder.com/question/next?pid=6291726&qid=112723&tid=9846874
[Programming questions] arithmetic progression
Time limit: 1 seconds
Space limit: 32768K If a sequence s satisfies for all the valid I, all have s[i + 1] = S[i] + D, where d can also be negative and 0, we call the sequence s as arithmetic progression.
Small easy now has a length of n series x, small Yi want to turn X into a arithmetic progression. Small-easy to allow the operation of an exchange of numeric values of any two positions on the sequence, and exchange operations allow switching several times. However, some of the series can be exchanged or not to become arithmetic progression, the small need to determine whether a sequence through the exchange of operations to become arithmetic progression
Enter a description:
The input consists of two rows, the first row containing the integer n (2≤n≤50), the length of the sequence. The
second row n elements x[i] (0≤x[i]≤1000), that is, each integer in the sequence.
Output Description:
If it can become arithmetic progression output "Possible", otherwise output "impossible".
Enter Example 1:
3
3 1 2
Output Example 1:
Possible
Package go.jacob.day813;
Import Java.util.Arrays;
Import Java.util.Scanner;
public class Demo3 {public
static void Main (string[] args) {
Scanner sc=new Scanner (system.in);
int N=sc.nextint ();
if (n<=2)
System.out.println ("Possible");
Int[] Arr=new int[n];
for (int i=0;i<n;i++) {
arr[i]=sc.nextint ();
}
Sc.close ();
Arrays.sort (arr);
int diff=arr[0]-arr[1];
for (int i=1;i<n-1;i++) {
if (arr[i]-arr[i+1]!=diff) {
System.out.println ("impossible");
return;
}
System.out.println ("Possible");
}