c++ - Maximise the AND -


Looking at an array of non-negative integers: A1, A2, ..., AN

Example: Let's have N = 4 and array [i.e. AO and AV] 2 4 8 10]. Answer is 8

Explanation

  2 and 4 = 0 2 and 8 = 0 2 and 10 = 2 4 and 8 = 0 4 and 10 = 0 8 and 10 = 8  

How to do it N 10 ^ 5 I can have the O (N ^ 2) solution. But its not efficient

code:

 for  (int i = 0; i & lt; n; i ++) {for (int j = i + 1 ; J & lt; n; j ++) {if (arr [i] and arr [j]> ans) {ans = arr [i] & amp; Arrival [ja];  
  1. Sort arrays in descending order
  2. < Li> Take the first two numbers. If they are both between two consecutive strengths of 2 (2 ^ Kashmir and 2 ^ (Kashmir + 1), then you can remove all elements with less than 2 ^ k.
  3. The remaining elements , Subtract 2 ^ from 2 ^ K.
  4. Repeat steps 2 and 3 until the number of elements in the array is 2.

Note: If you think That is the only major element is 2 ^ k and between 2 ^ (k + 1) and second largest element is less than 2 ^ k, then you will not remove any element, but only 2 ^ k It must be subtracted from the largest element.

Also, to determine which element exists in the series {1, 2, 4, 8, 16, ...} (O (Max) (log)), where the maximum number is the largest number in the array.


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 -