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.

saiujwal13083 | 347 |

isiddhisingh | 252 |

Abhishek_Jadhav | 243 |

_uncle_sam_ | 221 |

c_ocoooo | 217 |

saiujwal13083 | 610 |

SumitSingh27 | 466 |

c_ocoooo | 456 |

rathiarpit29 | 379 |

NaveenKumarNakka | 370 |

blackshadows | 5362 |

Ibrahim Nash | 5242 |

akhayrutdinov | 5111 |

mb1973 | 4931 |

Quandray | 4598 |

Login to report an issue on this page.