Please read the question and complete the incomplete code g
// Please read the question and complete the incomplete code given below, Thanks.
Consider the two arrays a and b.
a: 1 2 3 4 5
b: 3 4 5 1 2
It is possible to transform array a into array b by right shifting each element of a to the “right” three places. If an element “falls off” the back of the array have it come around the front and keep counting positions. That is how 3 in array ended up in the first position of array b. One way to look at this is to imagine that we are moving the element around in a circular manner. In the example above, we have right shifted the array 3 positions to the right. Definition: Let a and b be two integer arrays of the same length. We say that they are “shift equivalent” if array a can be right shifted to create array b. Problem Write a function bool equivalent(int a[], int b[], int n) which takes two arrays a and b of length n and returns true is they are shift equivalent and false otherwise
#include <iostream>
using namespace std;
void right_shift(int a[],int size){
…
…
}
bool equivalent(int a[],int b[],int size){
…
…
}
int main(){
int a[5]={1,2,3,4,5};
int b[5]={2,3,4,5,1};
bool isSame=false;
for(int i=0;i<5;i++){
if(equivalent(a,b,5))
isSame=true;
right_shift(a,5);
}
if(isSame)
cout<<\"a and b are equivalent.\"<<endl;
else cout<<\"a and b are not equivalent.\"<<endl;
return 0;
}
Solution
#include <iostream>
using namespace std;
bool equivalent(int a[],int b[],int size){
int count=0,j;
for(int i=0;i<size;i++)
{
j = (i+2)%size;
if(a[j]!=b[i])
{
return false;
}
}
return true;
}
int main(){
int a[5]={1,2,3,4,5};
int b[5]={3,4,5,1,2};
bool isSame=false;
isSame = equivalent(a,b,5);
if(isSame)
cout<<\"a and b are equivalent.\"<<endl;
else cout<<\"a and b are not equivalent.\"<<endl;
return 0;
}

