-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathequivalentArrays.cpp
More file actions
64 lines (42 loc) · 1.22 KB
/
Copy pathequivalentArrays.cpp
File metadata and controls
64 lines (42 loc) · 1.22 KB
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
/****************************
* Name: Julian Moskovits
* Description: Checks to see if 2 arrays are Shift Equivalent.
*****************************/
#include <iostream>
using namespace std;
bool equivalent(int a[], int b[], int n);
int main()
{
int x[5] = {1, 2, 3, 4, 5};
int y[5] = {3, 4, 5, 1, 2};
if (equivalent(x, y, 5) == true){
cout << "Array x and Array y are shift equivalent." << endl;
}
else {
cout << "Array x and Array y are NOT shift equivalent." << endl;
}
return 0;
}
bool equivalent(int a[], int b[], int n){
for (int rotation = 0; rotation < n; ++rotation){ //This loop checks if the 2 arrays are identical. If they are not it rotates array a and tries again.
int counter = 0;
for (int check = 0; check < n; ++check){ //This loop compares elements of array a to the coresponding elements in array b and counts how many are equivalent.
if (a[check] == b[check]){
++counter;
}
else {
break;
}
}
if (counter == n){
return true;
}
a: //If the arrays haven't been found to be equivalent, each element of array a is shifted one position to the left.
int temp = a[0];
for(int shift = 0; shift < n - 1; ++shift){
a[shift] = a[shift + 1];
}
a[n - 1] = temp;
}
return false;
}