Company: Amex SDE IIT Roorkee
Difficulty: medium
Task 2 Problem Description In base -2, integers are represented by sequences of bits in the following way. Bits are ordered from the least to the most significant. Sequence B of N bits represents the number: sum(B[i]*(-2)^i for i = 0..N-1). The empty sequence represents 0. For example: 100111 represents -23 001011 represents -12 10011 represents 9 001 represents 4, because: | 1 | -2 | 4 | -8 | 16 | -32 | ... | |---|----|---|----|----|-----|-----| | 1 | 1 | 0 | 0 | 1 | 1 | | = 1 + 0 + 0 + (-8) + 16 + (-32) = -23 | 1 | 1 | 0 | 1 | 0 | 0 | | = 1 + 0 + 0 + (-8) + 16 = -12 | 1 | 1 | 0 | 0 | 1 | | | = 1 + 0 + 0 + (-8) + 16 = 9 | 1 | 0 | 0 | | | | | = 1 + 0 + 0 + 4 = 4 Note that such a representation is suitable for both positive and negative integers. Write a function: vector solution(vector &A, vector &B); that, given two arrays of bits: A of length M, containing a sequence representing some integer X, and B of length N, containing a sequence representing some integer Y, returns the shortes