c - Fastest and most efficient way to find the maximum no. that can be obtained by performing bitwise and on 2 DISTINCT elements of array -


Looking at an array of non-negative integers, what is the fastest and most effective way to find the maximum number of arrays Can be obtained by performing bitwise on 2 different elements and (ie, and operator) performance?

This is my code so far:

 for  max = 0 (i = 0; i & lt; n; i ++) {for (j = i + 1; j & lt; n; j ++) {temp = a [i] & amp; A [ja]; If (temporary> maximum) maximum = temporary}}  

This is definitely the simplest way. I'm looking for a more efficient solution.

There is probably something that TRAI (actually a binary tree) is used to get maximum XOR elements of the array. Details for the maximum XOR solution can be found at

I hope I have the question right. Here's my solution:

You have an array of integers, they say they are not integers with integers because we are working with bitware operation. Let's think of them zero in binary representation and as a string of those people, and then put them on top of each other.

We now have their respective bits standing. Let's draw vertical lines, starting from the left column. If we ever encounter more or equal than a column with two 1 , then get out of every line that does not have 1 s

you Seeing where it is?

It will continue until we have only two and no lines, and it has not been denied. If we ever end up with something 2, then that means something went wrong:

  • Less than 2 means that we initially have fewer than 2 lines
  • More than 2 means that ...
    • If we were less than what we did initially, then all should be equal
    • If in fact we start However, it may be that all are same, or every possible pair is bitwise different, which means that every single pair is 0

Here's the code Wrote that I have followed the above argument:

  #include & lt; Stdio.h & gt; # Include & lt; Stdlib.h & gt; # Include & lt; Time.h> # Include & lt; Memory.h & gt; #define bit (_x_) (1U & lt; & lt; (_x_)) Empty filter arrow (unsigned int * arr, size_t size) {srand (time (NULL)); For (Int i = 0; I  gt; = 1) {int count = 0; Int * index = null; {If (for the resultant [i] and thibit) {index = reall (index, ++ calculation * size index) for (int i = 0; ii; lt; size; i ++); Index [count - 1] = ii; }} If (count> = 2) {size = count; Results for [i] = result oriented [index [i]]; (for int i = 0; i & lt; size; i ++); As a result = reel (as a result, size * size * as a result); } If (size == 2) {success = 1; break; } Free (index); } If (success) printf ("success!% U and% u", the resulting result [0], as a result [1]); Other printf ("Failure! Either all pairs are bitwise different, or there are less than 2 elements, or something else ..."); Turtle ('\ n'); Return 0; }  

There is only one during action:

I'm not sure that this is the best, but it should be better than checking everyone.


Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -