Computer Science, asked by kakondhar09, 5 months ago

Use coding...
We have a sequence p = {P1, P2, ..., Pn} which is a permutation of{1, 2, ..., N}.
You can perform the following operation at most once: choose integers i and j(1<=i<=j <=N), and
swap Pi and Pj. Note that you can also choose not to perform it.
Print YES if you can sort p in ascending order in this way, and no otherwise.
Constraints
All values in input are integers.
2 <=N<=50
pis a permutation of {1, 2, ..., N}.
Input
Input is given from Standard Input in the following format:
N
Pi P2 ... PN
Output
Print YES if you can sort p in ascending order in the way stated in the problem statement, and NO otherwise.​

Answers

Answered by sraswathy7
1

Answer:

thanks for your free points

Answered by khanabdullah3098
0

Answer:

#include<bits/stdc++.h>

using namespace std;

int main()

{

   int n;

   cin>>n;

   int a[n],b[n];

   for(int i=0;i<n;b[i]=a[i],++i) cin>>a[i];

   

   sort(a,a+n);

   int count = 0;

   for(int i=0;i<n;++i)

   {

       if(a[i]!=b[i]) count++;

   }

   if(count>2) cout<<"NO";

   else cout<<"YES";

   

return 0;

}

Explanation:

Similar questions