sir, i am doing this question and getting a runtime error for 4th test case . Can you provide me the 4th test case so that i can correct my code.
Unlock problem One test case failure
The test cases are not visible. You can share your code so that i can help you find the runtime error.
Here is my code if you feel like seeing it:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int N,K;
cin>>N>>K;
int arr[N+1],index[N+1];
for(int i=1;i<=N;i++)
{
cin>>arr[i];
index[arr[i]]=i;
}
int temp=N,i=1;
while(K && i<=N)
{
if(arr[i]!=temp)
{
int pos=index[temp];
index[arr[i]]=pos;
swap(arr[i],arr[pos]);
K--;
}
i++;
temp--;
}
for(int i=1;i<=N;i++)
cout<<arr[i]<<" ";
}
#include
#include<bits/stdc++.h>
using namespace std;
int main() {
int n,i;
long k;
int arr[100000];
cin>>n>>k;
priority_queue pq;
for(i=0;i<n;i++)
{
cin>>arr[i];
pq.push(arr[i]);
}
int search,index,start=0,temp;
while(k!=0)
{
search=pq.top();
pq.pop();
if(search!=arr[start]){
for( i=start;i<n;i++)
{
if(arr[i]==search)
{
index=i;
break;
}
}
temp=arr[start];
arr[start]=arr[index];
arr[index]=temp;
k--;
}
start++;
}
for(i=0;i<n;i++)
{
cout<<arr[i]<<" ";
}
return 0;
}