It's not possible to ensure the equality of two arrays in less than O(N) time
Certainly, most algorithms are smart enough to "return early" upon realizing that two elements are not equal:
bool is_equal(std::array<int, 10> const& a, std::array<int, 10> const& b) {
for(size_t index = 0; index < 10; index++)
if(a[index] != b[index])
return false; //Return Early
return true;
}
But in order to verify that the arrays are equal, each individual element must be compared to their corresponding element in the other array. There's no way around this.
You could try to be sneaky, by precomputing hashes for the arrays at the time you create them.
auto[array, hash] = create_array();
auto[array2, hash2] = create_array(opt);
if(hash != hash2) {
std::cout << "Arrays are not equal in O(1) time!" << std::endl;
} else {
std::cout << "Arrays *might be* equal in O(1) time!" << std::endl;
}
But note how I've hedged with the equality check. Even if you have a fast way to compute the hash of the array, you can't verify the arrays are equal; you can only verify they are unequal. If the hash is big enough that it is 100% guaranteed to be unique for every possible set of values the array might contain, then comparing those two hashes together will still be proportional to the sizes of the array. This might be feasible if the domain of the array is extremely constrained (like we know the array can only contain a very small subset of known values for each index) but that's only applicable to a very small subset of problems, which probably does not represent your problem domain.
You could still get a speedup with the hash:
auto[array, hash] = create_array();
auto[array2, hash2] = create_array(opt);
if(hash != hash2) {
std::cout << "Arrays are not equal in O(1) time!" << std::endl;
} else {
if(array == array2)
std::cout << "Arrays *are definitely* equal (... but in O(N) time...)" << std::endl;
else
std::cout << "Arrays are not equal in O(N) time." << std::endl;
}
But note you still haven't broken O(N) time, you've simply improved your best-case runtime.
So no, no matter how you try to cheat the code, it's not possible to have an accurate equality comparison check between two arrays without having a runtime of O(N).