-
Notifications
You must be signed in to change notification settings - Fork 3
/
IsArrayMirrorInverse.cpp
83 lines (62 loc) · 1.54 KB
/
IsArrayMirrorInverse.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
Arrays - Is Array Mirror Inverse
Input elements of array of size N.
Write a function that returns true if the array is mirror-inverse and false otherwise.
N cannot be negative. Range of numbers can be between 0 to N-1.
*/
#include<iostream>
using namespace std;
int main()
{
int N, arr[30];
bool x, lessThanN(int, int []), mirrorInverse(int, int []);
cout<<"Enter array size : ";
cin>>N;
cout<<endl<<"Enter array elements :\n";
for(int i=0; i<N; ++i)
cin>>arr[i];
x=lessThanN(N,arr); //elements in array should be between 0 to N-1
if(x!=0)
{
x=mirrorInverse(N,arr);
if(x!=0)
cout<<endl<<"TRUE"<<endl;
else
cout<<endl<<"FALSE"<<endl;
}
else
cout<<endl<<"FALSE"<<endl;
return 0;
}
bool lessThanN(int N, int arr[])
{
bool x, noRepeat(int, int []);
for(int i=0; i<N; ++i)
if(arr[i]<0||arr[i]>=N)
return false;
else
x=noRepeat(N,arr); //elements should be unique
return x;
}
bool noRepeat(int N, int arr[])
{
int i, j;
for(i=0; i<N; ++i)
for(j=i+1; j<N; ++j)
if(arr[j]==arr[i])
return false;
return true;
}
bool mirrorInverse(int N, int arr[])
{
int x, i, j;
for(i=0; i<N; ++i)
{
x=arr[i];
for(j=i+1; j<N; ++j)
if(arr[j]==i)
if(j!=x)
return false;
}
return true;
}