# Check if an array can be Arranged in Left or Right Positioned Array

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

## Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution.

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…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”.

## C++

 `// C++ program to check if an array can be arranged ` `// to left or right positioned array. ` `#include ` `using` `namespace` `std; ` ` `  `// Function to check Left or Right Positioned ` `// Array. ` `// arr[] is array of n elements ` `// visited[] is boolean array of size n ` `bool` `leftRight(``int` `arr[],``int` `n) ` `{ ` `    ``// Initially no element is placed at any position ` `    ``int` `visited[n] = {0}; ` ` `  `    ``// Traverse each element of array ` `    ``for` `(``int` `i=0; i

## Java

 `// Java program to check if an array ` `// can be arranged to left or ` `// right positioned array. ` ` `  `class` `GFG { ` `     `  `    ``// Function to check Left or  ` `    ``// Right Positioned Array. ` `    ``// arr[] is array of n elements ` `    ``// visited[] is boolean array of size n ` `    ``static` `boolean` `leftRight(``int` `arr[], ``int` `n) { ` `     `  `    ``// Initially no element is  ` `    ``// placed at any position ` `    ``int` `visited[] = ``new` `int``[n]; ` ` `  `    ``// Traverse each element of array ` `    ``for` `(``int` `i = ``0``; i < n; i++) { ` `         `  `    ``// Element must be smaller than n. ` `    ``if` `(arr[i] < n) { ` `         `  `        ``// Place "arr[i]" at position "i" ` `        ``// or at position n-arr[i]-1 ` `        ``if` `(visited[arr[i]] == ``0``) ` `        ``visited[arr[i]] = ``1``; ` `        ``else` `        ``visited[n - arr[i] - ``1``] = ``1``; ` `    ``} ` `    ``} ` ` `  `    ``// All positions must be occupied ` `    ``for` `(``int` `i = ``0``; i < n; i++) ` `    ``if` `(visited[i] == ``0``) ` `        ``return` `false``; ` ` `  `    ``return` `true``; ` `} ` ` `  `// Driver code ` `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"``); ` `} ` `} ` ` `  `// This code is contributed by Anant Agarwal. `

## Python3

 `# Python3 program to check ` `# if an array can be arranged ` `# to left or right positioned array. ` ` `  `# Function to check Left ` `# or Right Positioned ` `# Array. ` `# arr[] is array of n elements ` `# visited[] is boolean array of size n ` `def` `leftRight(arr,n): ` ` `  `    ``# Initially no element ` `    ``# is placed at any position ` `    ``visited``=``[] ` `    ``for` `i ``in` `range``(n``+``1``): ` `        ``visited.append(``0``) ` `  `  `    ``# Traverse each element of array ` `    ``for` `i ``in` `range``(n): ` `     `  `        ``# Element must be smaller than n. ` `        ``if` `(arr[i] < n): ` `         `  `            ``# Place "arr[i]" at position "i" ` `            ``# or at position n-arr[i]-1 ` `            ``if` `(visited[arr[i]] ``=``=` `0``): ` `                ``visited[arr[i]] ``=` `1` `            ``else``: ` `                ``visited[n``-``arr[i]``-``1``] ``=` `1` `  `  `    ``# All positions must be occupied ` `    ``for` `i ``in` `range``(n): ` `        ``if` `(visited[i] ``=``=` `0``): ` `            ``return` `False` `  `  `    ``return` `True` `     `  `# Driver code ` ` `  `arr ``=` `[``2``, ``1``, ``5``, ``2``, ``1``, ``5``] ` `n ``=` `len``(arr) ` ` `  `if` `(leftRight(arr, n) ``=``=` `True``): ` `    ``print``(``"YES"``) ` `else``: ` `    ``print``(``"NO"``) ` ` `  `# This code is contributed ` `# by Anant Agarwal. `

## C#

 `     `  `// C# program to check if an array ` `// can be arranged to left or ` `// right positioned array. ` `using` `System;  ` `public` `class` `GFG { ` ` `  `        ``// Function to check Left or  ` `        ``// Right Positioned Array. ` `        ``// arr[] is array of n elements ` `        ``// visited[] is boolean array of size n ` `        ``static` `bool` `leftRight(``int` `[]arr, ``int` `n) { ` ` `  `        ``// Initially no element is  ` `        ``// placed at any position ` `        ``int` `[]visited = ``new` `int``[n]; ` ` `  `        ``// Traverse each element of array ` `        ``for` `(``int` `i = 0; i < n; i++) { ` ` `  `        ``// Element must be smaller than n. ` `        ``if` `(arr[i] < n) { ` ` `  `            ``// Place "arr[i]" at position "i" ` `            ``// or at position n-arr[i]-1 ` `            ``if` `(visited[arr[i]] == 0) ` `            ``visited[arr[i]] = 1; ` `            ``else` `            ``visited[n - arr[i] - 1] = 1; ` `        ``} ` `        ``} ` ` `  `        ``// All positions must be occupied ` `        ``for` `(``int` `i = 0; i < n; i++) ` `        ``if` `(visited[i] == 0) ` `            ``return` `false``; ` ` `  `        ``return` `true``; ` `    ``} ` ` `  `    ``// Driver code ` `    ``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"``); ` `    ``} ` `} ` `// This code is contributed by PrinciRaj1992 `

## PHP

 ` `

Output :

```"YES"
```

Time Complexity : O(n)
Auxiliary Space : O(n)

This article is contributed by Shashank Mishra ( Gullu ). 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.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up

Improved By : jit_t, princiraj1992

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.