Check if an array can be Arranged in Left or Right Positioned Array
Last Updated :
05 Jul, 2022
Given an array arr[] of size n>4, the task is to check whether the given array can be arranged in the form of Left or Right positioned array?
Left or Right Positioned Array means each element in the array is equal to the number of elements to its left or number of elements to its right.
Examples :
Input : arr[] = {1, 3, 3, 2}
Output : "YES"
This array has one such arrangement {3, 1, 2, 3}.
In this arrangement, first element '3' indicates
that three numbers are after it, the 2nd element
'1' indicates that one number is before it, the
3rd element '2' indicates that two elements are
before it.
Input : arr[] = {1, 6, 5, 4, 3, 2, 1}
Output: "NO"
// No such arrangement is possible
Input : arr[] = {2, 0, 1, 3}
Output: "YES"
// Possible arrangement is {0, 1, 2, 3}
Input : arr[] = {2, 1, 5, 2, 1, 5}
Output: "YES"
// Possible arrangement is {5, 1, 2, 2, 1, 5}
A simple solution is to generate all possible arrangements (see this article) and check for the Left or Right Positioned Array condition, if each element in the array satisfies the condition then “YES” else “NO”. Time complexity for this approach is O(n*n! + n), n*n! to generate all arrangements and n for checking the condition using temporary array.
An efficient solution for this problem needs little bit observation and pen-paper work. To satisfy the Left or Right Positioned Array condition all the numbers in the array should either be equal to index, i or (n-1-i) and arr[i] < n. So we create an visited[] array of size n and initialize its element with 0. Then we traverse array and follow given steps :
- If visited[arr[i]] = 0 then make it 1, which checks for the condition that number of elements on the left side of array arr[0]…arr[i-1] is equal to arr[i].
- Else make visited[n-arr[i]-1] = 1, which checks for the condition that number of elements on the right side of array arr[i+1]…arr[n-1] is equal to arr[i].
- Now traverse visited[] array and if all the elements of visited[] array become 1 that means arrangement is possible “YES” else “NO”.
Implementation:
C++
#include<bits/stdc++.h>
using namespace std;
bool leftRight( int arr[], int n)
{
int visited[n] = {0};
for ( int i=0; i<n; i++)
{
if (arr[i] < n)
{
if (visited[arr[i]] == 0)
visited[arr[i]] = 1;
else
visited[n-arr[i]-1] = 1;
}
}
for ( int i=0; i<n; i++)
if (visited[i] == 0)
return false ;
return true ;
}
int main()
{
int arr[] = {2, 1, 5, 2, 1, 5};
int n = sizeof (arr)/ sizeof (arr[0]);
if (leftRight(arr, n) == true )
cout << "YES" ;
else
cout << "NO" ;
return 0;
}
|
Java
class GFG {
static boolean leftRight( int arr[], int n) {
int visited[] = new int [n];
for ( int i = 0 ; i < n; i++) {
if (arr[i] < n) {
if (visited[arr[i]] == 0 )
visited[arr[i]] = 1 ;
else
visited[n - arr[i] - 1 ] = 1 ;
}
}
for ( int i = 0 ; i < n; i++)
if (visited[i] == 0 )
return false ;
return true ;
}
public static void main(String[] args)
{
int arr[] = { 2 , 1 , 5 , 2 , 1 , 5 };
int n = arr.length;
if (leftRight(arr, n) == true )
System.out.print( "YES" );
else
System.out.print( "NO" );
}
}
|
Python3
def leftRight(arr,n):
visited = []
for i in range (n + 1 ):
visited.append( 0 )
for i in range (n):
if (arr[i] < n):
if (visited[arr[i]] = = 0 ):
visited[arr[i]] = 1
else :
visited[n - arr[i] - 1 ] = 1
for i in range (n):
if (visited[i] = = 0 ):
return False
return True
arr = [ 2 , 1 , 5 , 2 , 1 , 5 ]
n = len (arr)
if (leftRight(arr, n) = = True ):
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
public class GFG {
static bool leftRight( int []arr, int n) {
int []visited = new int [n];
for ( int i = 0; i < n; i++) {
if (arr[i] < n) {
if (visited[arr[i]] == 0)
visited[arr[i]] = 1;
else
visited[n - arr[i] - 1] = 1;
}
}
for ( int i = 0; i < n; i++)
if (visited[i] == 0)
return false ;
return true ;
}
public static void Main()
{
int []arr = {2, 1, 5, 2, 1, 5};
int n = arr.Length;
if (leftRight(arr, n) == true )
Console.WriteLine( "YES" );
else
Console.WriteLine( "NO" );
}
}
|
PHP
<?php
function leftRight( $arr , $n )
{
$visited [ $n ] = array (0);
for ( $i = 0; $i < $n ; $i ++)
{
if ( $arr [ $i ] < $n )
{
$visited [ $arr [ $i ]] = 1;
$visited [ $n - $arr [ $i ] - 1] = 1;
}
}
for ( $i = 0; $i < $n ; $i ++)
if ( $visited [ $i ] == 0)
return false;
return true;
}
$arr = array (2, 1, 5, 2, 1, 5);
$n = sizeof( $arr );
if (leftRight( $arr , $n ) == true)
echo "YES" ;
else
echo "NO" ;
?>
|
Javascript
<script>
function leftRight(arr, n) {
let visited = new Array(n);
for (let i = 0; i < n; i++) {
if (arr[i] < n) {
if (visited[arr[i]] == 0)
visited[arr[i]] = 1;
else
visited[n - arr[i] - 1] = 1;
}
}
for (let i = 0; i < n; i++)
if (visited[i] == 0)
return false ;
return true ;
}
let arr = [2, 1, 5, 2, 1, 5];
let n = arr.length;
if (leftRight(arr, n) == true )
document.write( "YES" );
else
document.write( "NO" );
</script>
|
Time Complexity : O(n)
Auxiliary Space : O(n)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...