Company: Amex SDE IIT Roorkee

Difficulty: medium

Problem Statement

Task 2 Problem Description In base -2, an integer is written as a sequence of bits ordered from least significant to most significant. A sequence B made of N bits stands for the value sum(B[i]*(-2)^i for i = 0..N-1), and the empty sequence stands for 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 This kind of representation works for both positive and negative integers alike. Write a function: vector solution(vector &A, vector &B); that, given two arrays of bits: A of length M, holding a sequence that represents some integer X, and B of length N, holding a sequence that represents some integer Y, returns the shortest bit sequence represent

More Amex SDE IIT Roorkee OA questionsInterview experiences