#K13346. Partition Array into Two Non-Decreasing Subarrays
Partition Array into Two Non-Decreasing Subarrays
Partition Array into Two Non-Decreasing Subarrays
You are given an array of n integers. Your task is to determine whether it is possible to partition the array into two contiguous subarrays such that each subarray is non-decreasing (i.e., each element is not less than the previous one).
Formally, given an integer n and an array \(A = [a_1, a_2, \dots, a_n]\), find an index \(i\) with \(1 \le i \le n-1\) such that:
[ \begin{aligned} a_1 &\le a_2 \le \dots \le a_i, \ a_{i+1} &\le a_{i+2} \le \dots \le a_n. \end{aligned} ]
If such an index exists, output YES
, otherwise output NO
. Note that an array with a single element cannot be partitioned.
inputFormat
The input is given via standard input (stdin) and consists of two lines:
- The first line contains an integer n representing the number of elements in the array.
- The second line contains n space-separated integers which represent the elements of the array.
outputFormat
Output a single line to standard output (stdout) containing either YES
if the array can be partitioned into two non-decreasing contiguous subarrays, or NO
otherwise.
5
2 2 3 1 4
YES