I have been taking tests for Internships and came across this coding problem, and I was not able to come up with a solution,
Also I have not seen this kind of question on Leetcode given that I have solved 250+ questions.
Problem:
You are given two Integers N and X.
You are required to create an array of N natural numbers such that *Bitwise XOR* of these numbers is equal to X.
And the *sum of all the elements* in array is as *minimum* as possible.
Consider Array in *non - decreasing* order.
If multiple array exist then print the smallest among them.
Input:
N=3, X=2
Output:
[1, 1, 2]
I have not come across these kind of questions on Leetcode, so any suggestions for learning these will be helpful.
Thanks!!
Want to see the real deal?
More inside scoop? View in App
More inside scoop? View in App
blind
SUPPORT
FOLLOW US
DOWNLOAD THE APP:
FOLLOWING
Industries
Job Groups
- Software Engineering
- Product Management
- Information Technology
- Data Science & Analytics
- Management Consulting
- Hardware Engineering
- Design
- Sales
- Security
- Investment Banking & Sell Side
- Marketing
- Private Equity & Buy Side
- Corporate Finance
- Supply Chain
- Business Development
- Human Resources
- Operations
- Legal
- Admin
- Customer Service
- Communications
Return to Office
Work From Home
COVID-19
Layoffs
Investments & Money
Work Visa
Housing
Referrals
Job Openings
Startups
Office Life
Mental Health
HR Issues
Blockchain & Crypto
Fitness & Nutrition
Travel
Health Care & Insurance
Tax
Hobbies & Entertainment
Working Parents
Food & Dining
IPO
Side Jobs
Show more
SUPPORT
FOLLOW US
DOWNLOAD THE APP:
comments
If N is even, fill N-2 1s and for the last two elements:
if X is odd:
Last two elements are X-1 and 1
If X is even:
Last two elements are X+1 and 1
The idea with filling 1s is that, we are using the lowest allowed value and they cancel each other out. But when you can’t fill in an even number of 1s, you then accommodate the last 1, by manipulating X (either removing the last bit or adding the last bit) such that the XOR will get us the final result X
So, the solution would be first spreading N's bits over as many elements as possible, and then place 1s at the rest of the elements (omitting edge cases for laziness).
So 0 cannot be used.
If num1s = n the array should be broken up by powers of 2 so each element gets a 1 in binary, e.g. for x=13,n=3 the solution is [8,4,1] (in binary, [1000,0100,0001] xor to 1101).
If num1s is greater than n some 1s need to be combined. Eg [1100,0001] xor to 1101.
If num1s is less than n some padding with 1s will need to be added. If n is greater than num1s by an even number, just add the padding, e.g. [1000,0100,0001,0001,0001] xor to 1101.
If odd, 1 will need to be added to another term, e.g [1001,0100,0001,0001].
However this will fail if x=1 and n is even. In this case we need a solution like [11,10] or [11,10,01,01] etc.
Thanks a lot.
Can you please tell how you learnt to come up with such approaches?
How much time did it take to be so good at problem solving and what platforms you use for coding?
Thanks!!
It's hard to give any general advice. I think as you learn algorithms and practice you find strategies for problem solving, and when you hear a new problem you try to fit it to one of those strategies.
I guess 4 years (college)? Algorithms class, CS competitions (ACM ICPC), and "Cracking the Coding Interview" (book). I didn't know about LC when I was interviewing a couple years ago.
[y,y,y,y,y,y,X] <--> y^y^y^y^y^y^X = 0 ^ X = X (that's the first part)
To minimize the sum, if 0 is allowed, then y = 0, otherwise you can use y = 1 and your answer will be as follows
[1,1,1,1,1,1,X]
Also i believe there is a similar problem in Leetcode, the one to find the only non duplicate integer in an array of duplicates. Solution is XOR all elements and the result is your unique number.
Hope it helps!
Edit: based on counter examples and other below comments, this doesn't solve the problem correctly with regards to minimizing the sum. Please check below for more accurate answers. Leaving this answer though for XOR part,
So we are trying to minimize the number of additional 1s we need to add, thus minimizing the total sum of the array