Problem · Bit Manipulation
Find Y Value
Learn this problemProblem statement
Given a binary number as a string, x (a binary string), return the binary string of the same length, y, that will produce the maximum value when XORed with x. There is a number of bits that may be set in y called maxSet.
The binary strings will always have bits digits, and leading zeros are fine.
Function
findYValue(bits: int, maxSet: int, x: String) → String
Complete the function findYValue in the editor below.
findYValue has the following parameter(s):
- 1.
int bits: the length of the binary stringsxandy - 2.
int maxSet: the number of bits that may be set iny - 3.
string x: a binary string
Returns
string: the best y value as a binary string
Examples
Example 1
bits = 3maxSet = 1x = "101"return = "010"First, determine all possible 000 xor 101 = 101
001 xor 101 = 100
010 xor 101 = 111
100 xor 101 = 001
The third value produces the maximal result, where
bits = 3 digit binary strings with only maxBits = 1 or fewer bits set: 000, 001, 010, 100. These are the potential y values.
Now, XOR each of the y values with x = 101:
y = 010. Return the string '010'.Constraints
Unknown for nowMore IBM problems
- Parent Process NumberOA · Seen Jul 2026
- Request Retry CountOA · Seen Jul 2026
- Count Strictly Increasing Subsequences of Length 3OA · Seen Jul 2026
- Maximum Requests in a Time WindowOA · Seen Jul 2026
- Query Type Frequency WindowOA · Seen Jul 2026
- Minimum Number of Non-Empty Disjoint SegmentsOA · Seen Jul 2026
- Spam Text ClassificationOA · Seen Jul 2026
- Count Ideal NumbersOA · Seen Jun 2026