Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9224319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:17:25+00:00 2026-06-18T04:17:25+00:00

Two arrays: a[] = {1 2 3 4} b[] = {3 4 1 2}

  • 0

Two arrays:

a[] = {1 2 3 4}
b[] = {3 4 1 2}

The bottom array is simply the top array shifted to the right two places. If the top array can be right shifted to create the bottom array, we call them shift equivalent.

Here is my attempt at creating a function (I need to use a Boolean function) to determine if two arrays are “shift equivalent”:

#include <iostream>
using namespace std;

bool equivalent(int a[], int b[], int size) {

    int value; // if 1 returns as truth
    int k; // counter to compare both arrays

    for (int j = 0; j <= size; j++) {
        for (int i = 0; i <= size; i++) {
            a[i] = a[i + j];
        }
    }

    for (k = 0; k <= size; k++) {
        if (a[k] != b[k]) {
            value = 0;
        } else value = 1;
    }

    return (value == 1);
}

int main() {
    int n;
    cout << "Please input a size " << endl;
    cin >> n;

    int *mtrx = new int[n];
    int *mtrx1 = new int[n];
    int x;
    for (x = 0; x < n; x++) {
        cout << "Please make entries for the first array: " << endl;
        cin >> mtrx[x];
    }
    x = 0;
    for (x = 0; x < n; x++) {
        cout << "Please make entries for the 2nd array: " << endl;
        cin >> mtrx1[x];
    }

    bool answr = equivalent(mtrx, mtrx1, n = n - 1);

    if (answr) {
        cout << "They are shift equivalent." << endl;
    } else {
        cout << "They are not shift equivalent." << endl;
    }

    delete[] mtrx;
    delete[] mtrx1;

    system("PAUSE");
    return 0;
}

When I execute my program, I use array1 = {1 2 3} and array2 = {3 1 2} to test shift equivalency. They are supposed to be, but my program says they aren’t.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T04:17:26+00:00Added an answer on June 18, 2026 at 4:17 am

    One problem which I see in your code is that you access memory beyond the array. If you add two indices, you have to make sure that they “wrap around” if you want to treat your array cyclic. This can be done with a modulo. Instead of

    a[i + j]
    

    you should write

    a[(i + j) % size]
    

    I’d split your algorithm into two parts: First, write a function which tests if a equals b with a shift of shift. Then, within a second function (the final equivalent function), test for all possible values for shift.

    bool equivalentFixed(int a[], int b[], int size, int shift) {
        for (int i = 0; i < size; ++i) {
            if (a[i] != a[(i + shift) % size])
                return false;
        }
        return true;
    }
    
    bool equivalent(int a[], int b[], int size) {
        for (int shift = 0; shift < size; ++shift) {
            if (equivalentFixed(a, b, size, shift))
                return true;
        }
        return false;
    }
    

    If you look close, you don’t see any local variable to hold (store) the value if the arrays are equivalent. Indeed, your code has a problem here, too, since you always overwrite the old status with the new one for each single entry you compare. So if the comparison fails anywhere during scanning the arrays, but the very last entry compares to equal, you return “yes, they are equal”, since you have overwritten the status.

    Now have a look at my equivalent implementation. I scan for different offsets (shift) if the arrays compare equal. Let’s focus on this function, not how the comparison is done in the other function. The point is: We have to return true if for any shift they compare equal, not for the last one and not for all.

    The idea to solve this is to break the loop (stop) if you found a solution. You can even return the whole function, since you know the complete return value, namely true.

    If for no possible shift the comparison is true, we didn’t find any possible shift, so they aren’t “shift equivalent”.

    I used a very similar approach to implement the comparison of two arrays with a fixed shift (equivalentFixed). Can you explain how this is done?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two arrays I need to compare.... $Drink Array ( [0] => Drink
I created the following array manually (bottom), I wish to create it programmatically using
Below are two 2D objects, Array and Vector. As you can see the information
Given two arrays, how to find the maximum element which is common to both
I have two arrays, @names and @employees , that are filled with strings that
I have the following two arrays , i am trying to see whether if
I have two arrays. I'd like to copy ranges of data from one of
I want to merge two arrays and replace the text with strtr function. I
I have two arrays and I post the arrays in one input; <input name='sistem[]'
I have two arrays in JavaScript: var xcoord = []; and var ycoord =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.