Find the maximum XOR among strong pairs in an array

Quick Overview

Given an integer array, find the largest bitwise XOR over all strong pairs, where two values form a strong pair when their difference is at most the smaller of the two. It tests careful handling of the pair condition, exhaustive pair enumeration and basic bit manipulation.

Find the maximum XOR among strong pairs in an array

Company: Zscaler

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

You are given an integer array `nums`. Two integers `x` and `y` form a **strong pair** when `|x - y| <= min(x, y)`. Select two integers from `nums` that form a strong pair and whose bitwise XOR is the largest among all strong pairs that can be formed from `nums`. Return that maximum XOR value. You may pick the same integer twice to form a pair. ### Function Signature ```python def maximum_strong_pair_xor(nums: list[int]) -> int: ``` ### Rules - A pair may use the same element twice, so `(x, x)` is always a strong pair with XOR `0`. - Return only the maximum XOR value, not the pair that produces it. ### Constraints - `1 <= len(nums) <= 50` - `1 <= nums[i] <= 100` ### Examples ```text Input: nums = [1,2,3,4,5] Output: 7 ``` The strong pairs of two different values are (1, 2), (2, 3), (2, 4), (3, 4), (3, 5) and (4, 5), with XORs 3, 1, 6, 7, 6 and 1. The largest is `3 XOR 4 = 7`. ```text Input: nums = [10,100] Output: 0 ``` `|10 - 100| = 90` is greater than `min(10, 100) = 10`, so (10, 100) is not a strong pair. Only (10, 10) and (100, 100) remain, and both have XOR 0. ```text Input: nums = [5,6,25,30] Output: 7 ``` Among pairs of different values, only (5, 6) and (25, 30) are strong. `5 XOR 6 = 3` and `25 XOR 30 = 7`, so the answer is 7.

Overview: Given an integer array, find the largest bitwise XOR over all strong pairs, where two values form a strong pair when their difference is at most the smaller of the two. It tests careful handling of the pair condition, exhaustive pair enumeration and basic bit manipulation.

Zscaler logo
Zscaler
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

You are given an integer array nums. Two integers x and y form a strong pair when |x - y| <= min(x, y).

Select two integers from nums that form a strong pair and whose bitwise XOR is the largest among all strong pairs that can be formed from nums. Return that maximum XOR value. You may pick the same integer twice to form a pair.

Function Signature

def maximum_strong_pair_xor(nums: list[int]) -> int:

Rules

  • A pair may use the same element twice, so (x, x) is always a strong pair with XOR 0 .
  • Return only the maximum XOR value, not the pair that produces it.

Constraints

  • 1 <= len(nums) <= 50
  • 1 <= nums[i] <= 100

Examples

Input: nums = [1,2,3,4,5]
Output: 7

The strong pairs of two different values are (1, 2), (2, 3), (2, 4), (3, 4), (3, 5) and (4, 5), with XORs 3, 1, 6, 7, 6 and 1. The largest is 3 XOR 4 = 7.

Input: nums = [10,100]
Output: 0

|10 - 100| = 90 is greater than min(10, 100) = 10, so (10, 100) is not a strong pair. Only (10, 10) and (100, 100) remain, and both have XOR 0.

Input: nums = [5,6,25,30]
Output: 7

Among pairs of different values, only (5, 6) and (25, 30) are strong. 5 XOR 6 = 3 and 25 XOR 30 = 7, so the answer is 7.