Given an array arr[] of size n>4, the task it 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 : "1"
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: "0"
// No such arrangement is possible
Input : arr[] = {2, 0, 1, 3}
Output: "1"
// Possible arrangement is {0, 1, 2, 3}
Input : arr[] = {2, 1, 5, 2, 1, 5}
Output: "1"
// Possible arrangement is {5, 1, 2, 2, 1, 5}
```

**Input:**

The first line of input contains an integer T denoting the number of test cases. The first line of each test case is N, size of array. The second line of each test case contains N input array elements.

**Output:**

Print 1 if array can be arranged to form left or right positioned array, else print 0.

**Constraints:**

1 ≤ T ≤ 100

4 < N ≤ 1000

1 ≤ arr[i] ≤ 1000

**Example:**

Input

2

5

1 2 3 5 0

9

1 2 3 4 0 5 6 7 8

Output

0

1

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

msriramvarma01 | 25 |

AbhilashKumar2 | 24 |

monusinghal | 24 |

tripurari_IT | 24 |

Abreez Das | 23 |

ARUN_PUNDIR | 609 |

manishgupta8497 | 607 |

KhyatiSoneji | 501 |

Abhimanyu_kumar | 487 |

vvggfggcg | 453 |

blackshadows | 5333 |

Ibrahim Nash | 5221 |

akhayrutdinov | 5111 |

mb1973 | 4929 |

Quandray | 4567 |

Login to report an issue on this page.