I have tried several testcases and compared using others code locally. I dont know why its giving wa.
Can anyone give a testcase which fails my code
#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define mod 1000000007
#define endl “\n”;
using namespace std;
int main() {
#ifndef ONLINE_JUDGE
freopen(“cfi.txt”,“r”,stdin);
freopen(“rel.txt”,“w”,stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll t;
cin>>t;
while(t--){
ll n,k;
cin>>n>>k;
ll a[n+1];
for(ll i=1;i<=n;i++){
cin>>a[i];
}
bool f1 = 1;
for(ll i=1;i<=n;i++){
if(a[i]!=i){
f1=0;
break;
}
}
if(f1){
cout<<0<<endl;
}
else if(n==k){
cout<<1<<endl;
}
else{
vector<bool> hash1(n+1,0);
vector<bool> hash2(n+1,0);
ll sub = n-k;
ll c = 0;
ll x = 1;
ll y = n;
for(ll i=1;i<=sub;i++){
if(a[i]!=i){
x = i;
break;
}
else{
c++;
hash1[a[i]] = 1;
}
}
ll sub1 = sub - c;
bool f3 = 1;
for(ll i=n;i>n-sub1;i--){
if(a[i]!=i){
f3 = 0;
y = i;
break;
}
else{
c++;
hash2[a[i]] = 1;
}
}
if(f3){
cout<<1<<endl;
}
else{
bool f4 = 1;
bool f5 = 1;
for(ll i=x;i<x+k;i++){
hash1[a[i]] = 1;
}
for(ll i=1;i<=sub;i++){
if(hash1[i]==0){
f4 = 0;
break;
}
}
for(ll i=y;i>y-k;i--){
hash2[a[i]] = 1;
}
for(ll i=n;i>n-sub;i--){
if(hash2[i]==0){
f5 = 0;
break;
}
}
if(f4 || f5){
cout<<2<<endl;
}
else{
cout<<3<<endl;
}
}
}
}
}