NetEase 2017 internal push [programming question] arithmetic progression @java__ algorithm

Source: Internet
Author: User
Tags arithmetic diff time limit

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");
		
	}






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.