Created
July 1, 2020 08:54
-
-
Save pratik7368patil/baa7a494cf1d42050ecce5438ba09c3a to your computer and use it in GitHub Desktop.
Solution for swap it problem in C++
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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