Last active
August 29, 2015 14:26
Given a permutation check if it is an ambiguous permutation. Solution for https://www.codechef.com/problems/PERMUT2
This file contains 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> | |
#include <vector> | |
using namespace std; | |
bool isAmbiguous(vector<int>& arr) | |
{ | |
int i; | |
vector<int> inverse(arr.size()); | |
for( i = 0; i < arr.size(); i++ ) | |
{ | |
inverse[arr[i]-1] = i+1; | |
} | |
for( i = 0; i < arr.size(); i++ ) | |
{ | |
if( arr[i] != inverse[i] ) | |
return false; | |
} | |
return true; | |
} | |
int main() | |
{ | |
int n; | |
while(true) | |
{ | |
cin >> n; | |
if( n > 0 ) | |
{ | |
vector<int> arr(n); | |
int i; | |
for( i = 0; i < n; i++ ) | |
{ | |
cin >> arr[i]; | |
} | |
if( isAmbiguous(arr) ) | |
{ | |
cout << "ambiguous" << endl; | |
} | |
else | |
{ | |
cout << "not ambiguous" << endl; | |
} | |
} | |
else | |
break; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment