Given an array **arr[] **consisting of **N** elements, the task is to check if the given array can be sorted by picking only corner elements i.e., elements either from left or right side of the array can be chosen.

**Examples:**

Input:arr[] = {2, 3, 4, 10, 4, 3, 1}Output:YesExplanation:

The order of picking elements from the array and placing in the sorted array are as follows:

{2, 3, 4, 10, 4, 3,1} -> {1}

{2, 3, 4, 10, 4, 3} -> {1, 2}

{3, 4, 10, 4, 3} -> {1, 2, 3}

{4, 10, 4,3} -> {1, 2, 3, 3}

{4, 10, 4} -> {1, 2, 3, 3, 4}

{10,4} -> {1, 2, 3, 3, 4, 4}

{10} -> {1, 2, 3, 3, 4, 4, 10}Input:a[] = {2, 4, 2, 3}Output:No

**Approach: **To solve the problem, we need to use a concept similar to Bitonic Sequence.Follow the below steps to solve the problem:

- Traverse the array and check if the sequence of array elements is decreasing, i.e. if the next element is smaller than previous element, then all the remaining elements should be decreasing or equal as well.
- That is, if the sequence is
**non-increasing**,**non-decreasing**or**non-decreasing followed by non-increasing**, only then the array can be sorted by the given operations.

Below is implementation of above approach:

## C++

`// C++ Program to implement ` `// the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to check if an array can ` `// be sorted using given operations ` `bool` `check(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `int` `i, g; ` ` ` `g = 0; ` ` ` `for` `(i = 1; i < n; i++) { ` ` ` ` ` `// If sequence becomes increasing ` ` ` `// after an already non-decreasing to ` ` ` `// non-increasing pattern ` ` ` `if` `(arr[i] - arr[i - 1] > 0 && g == 1) ` ` ` `return` `false` `; ` ` ` ` ` `// If a decreasing pattern is observed ` ` ` `if` `(arr[i] - arr[i - 1] < 0) ` ` ` `g = 1; ` ` ` `} ` ` ` `return` `true` `; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` ` ` `int` `arr[] = { 2, 3, 4, 10, 4, 3, 1 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(` `int` `); ` ` ` `if` `(check(arr, n) == ` `true` `) ` ` ` `cout << ` `"Yes"` ` ` `"\n"` `; ` ` ` `else` ` ` `cout << ` `"No"` ` ` `<< ` `"\n"` `; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to implement ` `// the above approach ` `class` `GFG{ ` ` ` `// Function to check if an array can ` `// be sorted using given operations ` `static` `boolean` `check(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `int` `i, g; ` ` ` `g = ` `0` `; ` ` ` ` ` `for` `(i = ` `1` `; i < n; i++) ` ` ` `{ ` ` ` ` ` `// If sequence becomes increasing ` ` ` `// after an already non-decreasing to ` ` ` `// non-increasing pattern ` ` ` `if` `(arr[i] - arr[i - ` `1` `] > ` `0` `&& g == ` `1` `) ` ` ` `return` `false` `; ` ` ` ` ` `// If a decreasing pattern is observed ` ` ` `if` `(arr[i] - arr[i - ` `1` `] < ` `0` `) ` ` ` `g = ` `1` `; ` ` ` `} ` ` ` `return` `true` `; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `arr[] = { ` `2` `, ` `3` `, ` `4` `, ` `10` `, ` `4` `, ` `3` `, ` `1` `}; ` ` ` `int` `n = arr.length; ` ` ` ` ` `if` `(check(arr, n) == ` `true` `) ` ` ` `{ ` ` ` `System.out.println(` `"Yes"` `); ` ` ` `} ` `else` ` ` `{ ` ` ` `System.out.println(` `"No"` `); ` ` ` `} ` `} ` `} ` ` ` `// This code is contributed by rutvik_56 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to implement ` `# the above approach ` ` ` `# Function to check if an array can ` `# be sorted using given operations ` `def` `check(arr, n): ` ` ` ` ` `g ` `=` `0` ` ` ` ` `for` `i ` `in` `range` `(` `1` `, n): ` ` ` ` ` `# If sequence becomes increasing ` ` ` `# after an already non-decreasing to ` ` ` `# non-increasing pattern ` ` ` `if` `(arr[i] ` `-` `arr[i ` `-` `1` `] > ` `0` `and` `g ` `=` `=` `1` `): ` ` ` `return` `False` ` ` ` ` `# If a decreasing pattern is observed ` ` ` `if` `(arr[i] ` `-` `arr[i] < ` `0` `): ` ` ` `g ` `=` `1` ` ` ` ` `return` `True` ` ` `# Driver Code ` `arr ` `=` `[ ` `2` `, ` `3` `, ` `4` `, ` `10` `, ` `4` `, ` `3` `, ` `1` `] ` `n ` `=` `len` `(arr) ` ` ` `if` `(check(arr, n) ` `=` `=` `True` `): ` ` ` `print` `(` `"Yes"` `) ` `else` `: ` ` ` `print` `(` `"No"` `) ` ` ` `# This code is contributed by Shivam Singh` |

*chevron_right*

*filter_none*

## C#

`// C# program to implement ` `// the above approach ` `using` `System; ` `class` `GFG{ ` ` ` `// Function to check if an array can ` `// be sorted using given operations ` `static` `bool` `check(` `int` `[]arr, ` `int` `n) ` `{ ` ` ` `int` `i, g; ` ` ` `g = 0; ` ` ` ` ` `for` `(i = 1; i < n; i++) ` ` ` `{ ` ` ` ` ` `// If sequence becomes increasing ` ` ` `// after an already non-decreasing to ` ` ` `// non-increasing pattern ` ` ` `if` `(arr[i] - arr[i - 1] > 0 && g == 1) ` ` ` `return` `false` `; ` ` ` ` ` `// If a decreasing pattern is observed ` ` ` `if` `(arr[i] - arr[i - 1] < 0) ` ` ` `g = 1; ` ` ` `} ` ` ` `return` `true` `; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `[]arr = { 2, 3, 4, 10, 4, 3, 1 }; ` ` ` `int` `n = arr.Length; ` ` ` ` ` `if` `(check(arr, n) == ` `true` `) ` ` ` `{ ` ` ` `Console.WriteLine(` `"Yes"` `); ` ` ` `} ` `else` ` ` `{ ` ` ` `Console.WriteLine(` `"No"` `); ` ` ` `} ` `} ` `} ` ` ` `// This code is contributed by sapnasingh4991 ` |

*chevron_right*

*filter_none*

**Output:**

Yes

**Time Complexity: **O(N) **Auxiliary Space:** O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Check if the array can be sorted only if the elements on given positions can be swapped
- Maximize sum of K elements in Array by taking only corner elements
- Maximize sum of atmost K elements in array by taking only corner elements | Set 2
- Maximize the sum of X+Y elements by picking X and Y elements from 1st and 2nd array
- Check if the array can be sorted using swaps between given indices only
- Maximum sum by picking elements from two arrays in order
- Maximum sum from three arrays such that picking elements consecutively from same is not allowed
- Maximum sum by picking elements from two arrays in order | Set 2
- Maximum area rectangle by picking four sides from array
- Combinations from n arrays picking one element from each array
- Sort an almost sorted array where only two elements are swapped
- Count of only repeated element in a sorted array of consecutive elements
- Maximum number of partitions that can be sorted individually to make sorted
- Check if a path exists for a cell valued 1 to reach the bottom right corner of a Matrix before any cell valued 2
- Remove k corner elements to maximize remaining sum
- Check if array can be sorted by swapping pairs having GCD equal to the smallest element in the array
- Check if array can be sorted by swapping pairs with GCD of set bits count equal to that of the smallest array element
- Check if Array elements can be maximized upto M by adding all elements from another array
- Count subarrays consisting of only 0's and only 1's in a binary array
- Generate all possible sorted arrays from alternate elements of two given sorted arrays

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.