Editorial
Applying an oracle to the state results in the following:
Next, by applying a gate to the rightmost qubit, we get:
Applying the oracle again results in the following, realizing the desired operation.
he circuit diagram for the case where is shown below.

Sample Code
Below is a sample program:
from qiskit import QuantumCircuit, QuantumRegister
def solve(n: int, o: QuantumCircuit) -> QuantumCircuit:
x, y = QuantumRegister(n), QuantumRegister(1)
qc = QuantumCircuit(x, y)
qc.compose(o, inplace=True)
qc.z(y)
qc.compose(o, inplace=True)
return qc