Equivalent Array's

My program is suppose to create a function that takes two array's and assigns them the same number of elements. Next it compares both arrays to decide if the array b can be created by right shifting array a. I've figured out that this shifting cannot exceed the number of element. However I am stuck, Im not sure of the steps i need to use in order to test if they are shift equivalent

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  
#include "stdafx.h"
#include <iostream>
using namespace std;

bool equivalent(int a[], int b[], int n) {
	for (int i = 0; i < n; i++)
	{
		a[i];
		b[i];
	}

}

int main()
{
    return 0;
}
If I understood you correctly, these two arrays are shift equivalent:
a = 1 2 3 4 5 6
b = 6 1 2 3 4 5
?

1
2
3
4
5
6
7
8
9
10
11
12
bool equivalent(int a[], int b[], int n) {
    int i = 0, j = 1;
    while (i <= n - 1) {
        if (a[i] != b[j])
            return false;
        i++;
        j++;
        if (j == n)
            j = 0;
    }
    return true;
}


So after comparing two elements function increments indices by 1.
When j becomes equal to number of elements function resets it to 0.
Last edited on
Topic archived. No new replies allowed.