## GUPTA MECHANICAL

IN THIS WEBSITE I CAN TELL ALL ABOUT TECH. TIPS AND TRICKS APP REVIEWS AND UNBOXINGS ALSO TECH. NEWS .............

# [Solution] End Sorted CodeChef Solution

Chef considers a permutation $P$ of $\left\{1,2,3,\dots ,N\right\}$ End Sorted if and only if ${P}_{1}=1$ and ${P}_{N}=N$.

Chef is given a permutation $P$.

In one operation Chef can choose any index  and swap ${P}_{i}$ and ${P}_{i+1}$. Determine the minimum number of operations required by Chef to make the permutation $P$ End Sorted.

Note: An array $P$ is said to be a permutation of $\left\{1,2,3,\dots ,N\right\}$ if $P$ contains each element of $\left\{1,2,3,\dots ,N\right\}$ exactly once.

### Input Format

• The first line of input will contain a single integer $T$, denoting the number of test cases.
Solution Click Below:-  👉
👇👇👇👇👇

• Each test case consists of two lines of input.
• The first line of each test case contains a single integer $N$, denoting the length of the permutation $P$.

• The second line contains $N$ space-separated integers ${P}_{1},{P}_{2},{P}_{3},\dots ,{P}_{N}$, denoting the permutation $P$.

### Output Format

For each test case, output minimum number of operations required by Chef to make the permutation $P$ End Sorted.

### Constraints

• $1\le T\le 1000$
• $2\le N\le {10}^{5}$