Company: Rippling

Difficulty: hard

Problem Statement

You are given an array arr of n non-negative integers. You repeat the following operation until only one element is left in the array: Choose any two different positions holding the values x and y . Add x XOR y (bitwise exclusive OR) to your score. Remove either x or y from the array (you choose which one). Exactly n - 1 operations are performed. Print the maximum total score that can be obtained. Input The first line contains a single integer n , the number of elements. The second line contains n space-separated integers arr[0], arr[1], …, arr[n-1] . Output Print a single integer — the maximum achievable score. Constraints 2 ≤ n ≤ 2000 0 ≤ arr[i] ≤ 10 9 Example Input: 5 1 2 3 4 5 Output: 25 Explanation: one optimal sequence of operations is: Pick 2 and 5, score += 2 XOR 5 = 7, remove 5 → [1, 2, 3, 4] Pick 3 and 4, score += 3 XOR 4 = 7, remove 3 → [1, 2, 4] Pick 2 and 4, score += 2 XOR 4 = 6, remove 2 → [1, 4] Pick 1 and 4, score += 1 XOR 4 = 5,

More Rippling OA questionsInterview experiences