Python provides an example to flip an array.

Source: Internet
Author: User

Python provides an example to flip an array.

This example describes how to use Python to flip an array. We will share this with you for your reference. The details are as follows:

Description

Given an integer array a with a length of n, the elements are not the same. to check whether such a segment exists in the array, you can only flip the segment to sort the entire array in ascending order. The array fragment [l, r] indicates the sequence a [l], a [l + 1], sequence..., and sequence a [r]. The original array is
A [1], a [2],..., a [L-2], L-1], a [l], a [l + 1],..., a [r-1], a [r], a [r + 1], a [r + 2],..., a [n-1], a [n],
The array after the reverse order of the [l, r] segment is
A [1], a [2],..., a [L-2], L-1], a [r], a [r-1],..., a [l + 1], a [l], a [r + 1], a [r + 2],..., a [n-1], a [n].

Input

The first row of data is an integer: n (1 ≤ n ≤ 105), indicating the length of the array.
The second row of data is n integers a [1], a [2],..., a [n] (1 ≤ a [I] ≤ 109 ).

Output

Output "yes". If yes; otherwise output "no", no quotation marks are required.

Sample output
Yes

Sample Input
4
2 1 3 4

Implementation Code:

While 1: n = int (raw_input () x = raw_input () breakx = [int (I) for I in x. split ()] y = [I for I in x] y. sort () partx = [x [I] for I in range (n) if x [I]! = Y [I] party = [y [I] for I in range (n) if x [I]! = Y [I] party. reverse () if partx = party: print "yes" else: print "no"

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.