TopcoderArchive
TCO15 Apr 2019
TCO19 Round 1A

EllysAndXor

Problem statement, definition, constraints, and public examples.

Problem Statement

Elly has a sequence of small positive integers. You are given this sequence in the int[] numbers. The girl wants to place a bitwise AND operator ('&') or a bitwise XOR operator ('^') between each pair of neighboring numbers. Now she wonders what is the maximal result that can be achieved if using optimal placement of operations between the integers?

For this task we will assume that the operations AND and XOR have equal priority (precedence), thus the expression is evaluated left to right. For example, 42 ^ 26 & 38 will be evaluated as (42 ^ 27) & 38 = 49 & 38 = 32. Note, however, that the maximal answer for the numbers (42, 27, 38) is achieved by using the expression 42 & 27 ^ 38 = (42 & 27) ^ 38 = 10 ^ 38 = 44.

Definition

Class:
EllysAndXor
Method:
getMax
Parameters:
int[]
Returns:
int
Method signature:
int getMax(int[] numbers)
(be sure your method is public)

Notes

  • The result of the bitwise operation AND (denoted as '&') applied on two integers is a new integer, having 1-bits on each position where both integers had a 1 in their binary representation, and 0 on all others. For example, 42(10) & 27(10) = 101010(2) & 011011(2) = 001010(2) = 10(10).
  • The result of the bitwise operation XOR (denoted as '^') applied on two integers is a new integer, having 1-bits on each position where exactly one of the integers had a 1 in their binary representation, and 0 on all others. For example, 42(10) ^ 27(10) = 101010(2) ^ 011011(2) = 110001(2) = 49(10).

Constraints

  • numbers will contain between 1 and 10 elements, inclusive.
  • Each element of numbers will be between 1 and 1000, inclusive.

Examples

  1. {42, 27, 38}
    Returns: 44
    The example from the problem statement.
  2. {666, 133, 438, 123, 893, 674, 462, 209}
    Returns: 976
  3. {42}
    Returns: 42
    The result of an expression with a single number equals the number.
  4. {123, 456, 789, 987, 654, 321}
    Returns: 975
  5. {42, 13, 17, 666, 811, 314, 1, 5, 2, 1000}
    Returns: 1007
    Please note that the answer can go over 1000.
Back to all problems