FastPrepFastPrep
Problem Brief

Find Y Value

FULLTIMEOA
See IBM online assessment and hiring insights

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 Description

Complete the function findYValue in the editor below.

findYValue has the following parameter(s):

  1. 1. int bits: the length of the binary strings x and y
  2. 2. int maxSet: the number of bits that may be set in y
  3. 3. string x: a binary string

Returns

string: the best y value as a binary string

1Example 1

Input
bits = 3, maxSet = 1, x = "101"
Output
"010"
Explanation
First, determine all possible 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:
  • 000 xor 101 = 101
  • 001 xor 101 = 100
  • 010 xor 101 = 111
  • 100 xor 101 = 001
  • The third value produces the maximal result, where y = 010. Return the string '010'.

    Constraints

    Limits and guarantees your solution can rely on.

    Unknown for now
    public String findYValue(int bits, int maxSet, String x) {
        // write your code here
    }
    
    Input

    bits

    3

    maxSet

    1

    x

    "101"

    Output

    "010"

    Sign in to submit your solution.