Algorithms and Data Structures

  • Profound Academy

    • Status
      • 1
        Implementation
      • 2
        Bitwise operations
      • 3
        Prefix Sums
      • 4
        Sliding window / Two pointers
      • 5
        Modular Arithmetic
      • 6
        Number Theory
      • 7
        Binary Search
      • 8
        Basic Sorting
      • 9
        Greedy Algorithms
      • 10
        Basic Dynamic Programming
      • 11
        Recursion
      • 12
        Linked LIst
      • 13
        Queue & Stack
      • 14
        Binary tree + BST
      • 15
        Divide & Conquer + Advanced Sorting
      • 16
        Heap
      • 17
        Hashing
      • 18
        Graph Representation
      • 19
        BFS

  • Bitwise operations

    Sometimes we need to perform operations on pairs of binary numbers. If we have two numbers a and b, we might want to have all the bits set to 0s except the ones that are 1 in both a and b (this corresponds to an AND operation). We might otherwise want to have all ones in positions where either a or b have a 1 there (this corresponds to an OR operation).
    a
    b
    a & b (AND)
    a | b (OR)
    a ^ b (XOR) exclusive OR
    binary and decimal representations
    binary and decimal representations
    1 if both and are 1 and 0 otherwise
    1 if either or are 1 and 0 otherwise
    1 if bits are different and 0 otherwise
    110 (6)
    101 (5)
    100 (4)
    111 (7)
    011 (3)
    100111 (39)
    010100 (20)
    100 (4)
    110111 (55)
    110011 (51)

    Challenge

    Given n integers, you are asked to find 2 of those that after applying OR to them would result in the largest number.

    Input

    The first line of the input contains a single integer n (1 ≤ n ≤ 1000).
    The next line contains n space-separated integers (1 ≤ ).

    Output

    The output should contain the largest value you can obtain by applying an OR operation on 2 values from the given numbers.

    Examples

    Input
    Output
    5 1 2 3 4 5
    7

    Explanation

    We can obtain 7 by applying OR to:
    • 2 (010) and 5 (101) → 7 (111)
    • 3 (011) and 4 (100) → (111)

    Constraints

    Time limit: 1 seconds

    Memory limit: 512 MB

    Output limit: 1 MB

    To check your solution you need to sign in
    Sign in to continue