Related Articles
Check if an Array can be Sorted by picking only the corner Array elements
• Difficulty Level : Easy
• Last Updated : 18 Aug, 2020

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: Yes
Explanation:
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 ``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;``}`

## 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`

## 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`

## 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`

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.

My Personal Notes arrow_drop_up