Check if an Array can be Sorted by picking only the corner Array elements
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++
#include <bits/stdc++.h>
using namespace std;
bool check( int arr[], int n)
{
int i, g;
g = 0;
for (i = 1; i < n; i++) {
if (arr[i] - arr[i - 1] > 0 && g == 1)
return false ;
if (arr[i] - arr[i - 1] < 0)
g = 1;
}
return true ;
}
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
class GFG{
static boolean check( int arr[], int n)
{
int i, g;
g = 0 ;
for (i = 1 ; i < n; i++)
{
if (arr[i] - arr[i - 1 ] > 0 && g == 1 )
return false ;
if (arr[i] - arr[i - 1 ] < 0 )
g = 1 ;
}
return true ;
}
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" );
}
}
}
|
Python3
def check(arr, n):
g = 0
for i in range ( 1 , n):
if (arr[i] - arr[i - 1 ] > 0 and g = = 1 ):
return False
if (arr[i] - arr[i] < 0 ):
g = 1
return True
arr = [ 2 , 3 , 4 , 10 , 4 , 3 , 1 ]
n = len (arr)
if (check(arr, n) = = True ):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG{
static bool check( int []arr, int n)
{
int i, g;
g = 0;
for (i = 1; i < n; i++)
{
if (arr[i] - arr[i - 1] > 0 && g == 1)
return false ;
if (arr[i] - arr[i - 1] < 0)
g = 1;
}
return true ;
}
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" );
}
}
}
|
Javascript
<script>
function check(arr, n)
{
var i, g;
g = 0;
for (i = 1; i < n; i++) {
if (arr[i] - arr[i - 1] > 0 && g == 1)
return false ;
if (arr[i] - arr[i - 1] < 0)
g = 1;
}
return true ;
}
var arr = [ 2, 3, 4, 10, 4, 3, 1 ];
var n = arr.length;
if (check(arr, n) == true )
document.write( "Yes" +
"<br>" );
else
document.write( "No"
+ "<br>" );
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
29 Apr, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...