Skip to content

Instantly share code, notes, and snippets.

@pratik7368patil
Created July 1, 2020 08:54
Show Gist options
  • Save pratik7368patil/baa7a494cf1d42050ecce5438ba09c3a to your computer and use it in GitHub Desktop.
Save pratik7368patil/baa7a494cf1d42050ecce5438ba09c3a to your computer and use it in GitHub Desktop.
Solution for swap it problem in C++
#include <iostream>
using namespace std;
int main() {
int N;
cin >> N; // Number if values
int value;
int count = 0 ;
for(int i = 0; i < N; i++){
cin >> value; // input sequence
if(value != i+1){
count++; // count if they are not in their position
}
}
// that means their are only two element that are not on their position
// for one swapping we need only two elements.
if(count <= 2) {
cout << "YES";
} else {
cout << "NO";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment