简体   繁体   中英

How do I compare two arrays that are equal but one is in ascending order and other is in descending order?

int array1[] = {1,2,3,4,5};
int array2[] = {5,4,3,2,1};
if (std::equal(std::begin(array1), std::end(array1), std::begin(array2)))
    cout << "Arrays are equal.";
else
    cout << "Arrays are not equal.";

Now both the arrays are equal but one is in ascending and the other one is descending so the output with this code is "Arrays are not equal", how can we output "Arrays are equal"?

Use reverse_iterator :

std::equal(std::begin(array1), std::end(array1), std::rbegin(array2))

If you know that one is always in ascending order, and the other in descending order then it's pretty easy to use forward iterators for one and reverse iterators for the other.

My guess would be that inverted order was only an example of containing the same data, but in a different order, and they both happened to be in ascending order, you'd still want them to compare as equal. And likewise, if both were in random order, but still contained exactly the same elements, you'd want that counted as equal as well.

In this case, you're pretty much stuck with comparing sorted copies of the two inputs. In theory you could step through one, and search for the elements in the other, but that's slow, and requires quite a bit of care to get correct (eg, unless you're careful, it's easy to end up with an implementation that says [1, 1, 2] is equal to [1, 2, 2] ). Worse, even if you get this really correct, you still end up with an O(N 2 ) algorithm, which gets pretty slow in a hurry if you have very many elements.

So, if you really need to support this, the most practical method is probably going to be to create a copy of each input, sort those copies, and then compare the results to each other (though if you can sort the originals, that obviously reduces memory usage).

If you're dealing with large arrays, you might also consider counting the number of times each value occurs using something like an std::unordered_map . This reduces the expected complexity from O(N log N) to O(N), so with enough elements, it should be faster (but exactly how many is enough may be hard to guess, and likely varies between implementations).

From C++20, you can use the ranges facilities to write the condition as:

std::ranges::equal(array1, std::views::reverse(array2))

which expresses the intent much more directly, and clearly.

See There are two approaches to solve the question.

  1. Sort anyone array...for eg. sort descending array. Then compare both the arrays. But sort function will take O(n log n) time.

  2. Traverse (Faster Approach) - In this approach, let there be two array A1 and A2. So travel in A1 from index 0 to length of A1. and travel in A2 from the length of A2 to 0. if elements are equal then yes both arrays have the same elements else not.

int array1[]= {1,2,3,4,5}
int array2[]= {5,4,3,2,1}

bool is_same = true;

for(int i=0; i<5; i++)
{
    if(array1[i] != array2[4-i])
    {
        is_same = false;
        break;
    }
}
if (is_same)
    cout << "Arrays are equal.";
else
    cout << "Arrays are not equal.";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM