Untitled

 avatar
meda
c_cpp
a month ago
817 B
7
Indexable
#include<bits/stdc++.h>
#define ll long long
#define endl "\n"
using namespace std;
template<class T>
void printff(vector<T>& v) {
  for (auto k : v) cout << k << " ";
  cout << endl;
}
void SOLVE() {
  int n; cin >> n;
  vector<int> a(n), sorted;
  for(auto & val : a) cin >> val;
  sorted = a;
  sort(sorted.begin(), sorted.end());
  int l = -1, r = n;
  for(int i = 0; i < n; i++){
    if(a[i] != sorted[i]){
        l = i;
        break;
    }
  }
  if(l == -1) return void(cout << 0 << endl);
  for(int i = n - 1; i >= 0 ; i--){
    if(sorted[i] != a[i]){
        r = i;
        break;
    }
  }
  cout << r - l + 1 << endl;
}
int main() {
  ios_base::sync_with_stdio(false); 
  cin.tie(NULL); 
  cout.tie(NULL);
  int tc = 1; cin >> tc;
  while(tc--) SOLVE();
  return 0;
}
Leave a Comment