Ctrl K

The Most Different Code

Hard
You are given an array codes of bits-bit binary codes: every number is between 0 and 2^bits - 1. The distance between two codes is the number of bit positions where they differ (numbers are padded with leading zeros to bits positions). For each code codes[i], find the largest distance between it and any other element codes[j] of the array (j ≠ i; the values themselves may be equal). Return an array of answers with the same length as codes.
Example 1
codes=[5, 2, 7], bits=3 → [3, 3, 2]

In binary these are 101, 010 and 111. Codes 101 and 010 differ in all three positions. For 111 the farthest code is 010, which differs in 2 positions.

Example 2
codes=[0, 0], bits=2 → [0, 0]

Both codes are equal, so the distance between them is 0.

Constraints
  • 1 ≤ bits ≤ 17
  • 2 ≤ len(codes) ≤ 2^bits
  • 0 ≤ codes[i] < 2^bits
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsKeys and Doors in a Maze HardTurn Every Lamp Off HardKnight Versus Pawns HardFewest Cross Strikes MediumLetter Order From a Dictionary Hard
loading Python…
Input
codes=[5, 2, 7], bits=3
Expected
[3, 3, 2]

Run checks the first 3 tests in your browser. Submit checks all 70 tests on the server, hidden ones included.