c++ - Compare two integer arrays and check if they have equal values -


I want to know what is the best way to compare two unsorted arrays and if Are they the same value using C ++? I have found a solution in O (NILAN) but is there an O (N) solution for this? How, if yes?

Please tell me that this is not a university question, I have the information to solve the problem of ACM.

Thank you.

Div>

It depends on your values ​​and how much of your memory is there to spend on this one for arrays (N) solution in which <; = X & lt; In the unique integers are x; In C, you can use an Assistant array of four count [C] and do something like this:

  int array1 [N]; Int array 2 [N]; // get values ​​in array 1 and 2 with a few quarters [c] {0}; {Count [array1 [i]] ++; (size_t i = 0; i & lt; n; i ++) for [Array2 [i]] ++ count; }  

Now, two arrays will be equal if and if all the elements of count are 0 or 2 then it can be checked in O (n) By running the same loop again, only this time, the value of counts [array1 [i]] (and the same as array2 ) is checked to be equal to 2. The same technique can be adapted for some other scenarios: For example, if the values ​​are unique, then you have to rely on two different arrays and then see the two calculations for equality However, if the number of potential values ​​is too much If you are large, then you have to resort to your information ('X is included in') in a separate structure where operation is in O (n log n), so that you are returning where you start Have.


Comments

Popular posts from this blog

c# - SignalR: "Protocol error: Unknown transport." when navigating to hub -

c# - WPF Expander overlay used in ItemsControl -

android - how to get distance of 2 beacons and make a condtion base on that distance -