## Amazon Interview Question

Quality Assurance Engineers**Team:**Kindle

**Country:**India

**Interview Type:**Written Test

```
var list = new List<int>() {1, 1, -11, 1, 12};
var getListCount = list.Count;
var isListCountOdd = getListCount % 2 == 0;
var getIntegerIncrement = 3;
int takeFromList;
if (isListCountOdd)
{
takeFromList = getListCount/2;
}
else
{
if (getListCount <= 5)
{
takeFromList = getListCount - getIntegerIncrement;
}
else
{
getIntegerIncrement++;
takeFromList = getListCount - getIntegerIncrement;
}
}
// Get left and right numbers
var leftNumbers = list.Take(takeFromList);
var rightNumbers = list.Skip(takeFromList).Take(getIntegerIncrement);
// Sum them up
var leftNumbersTotal = leftNumbers.Sum();
var rightNumbersTotal = rightNumbers.Sum();
Console.Write(leftNumbersTotal == rightNumbersTotal ? "Left == Right" : "Left != Right");
Console.ReadKey();
```

```
-- This solution is for list contains 4 or more count.
var list = new List<int>() {1, 1, -11, 1, 12};
var getListCount = list.Count;
var isListCountOdd = getListCount % 2 == 0;
var getIntegerIncrement = 3;
int takeFromList;
if (isListCountOdd)
{
takeFromList = getListCount/2;
}
else
{
if (getListCount <= 5)
{
takeFromList = getListCount - getIntegerIncrement;
}
else
{
getIntegerIncrement++;
takeFromList = getListCount - getIntegerIncrement;
}
}
// Get left and right numbers
var leftNumbers = list.Take(takeFromList);
var rightNumbers = list.Skip(takeFromList).Take(getIntegerIncrement);
// Sum them up
var leftNumbersTotal = leftNumbers.Sum();
var rightNumbersTotal = rightNumbers.Sum();
Console.Write(leftNumbersTotal == rightNumbersTotal ? "Left == Right" : "Left != Right");
Console.ReadKey();
```

PYTHON::

>>> def sum(l):

a = 0

d = 0

b = len(l)

c = b/2

for i in range(0, c):

a = a + l[i]

for j in range(c, ((c*2)+1)):

d = d + l[j]

if a == d:

print "both sides of sum is equal"

return a

elif a != d:

a = 0

d = 0

for i in range(0, c+1):

a = a + l[i]

for j in range(c+1, ((c*2)+1)):

d = d + l[j]

if a == d:

print "both sides of sum is equal at 2nd level"

return a

else:

print "both sides of sum is not equal"

return False

>>>

>>> sum([1,1,-11,1,12])

both sides of sum is equal

2

>>> sum([-11,1,12,1,1])

both sides of sum is equal at 2nd level

2

>>> sum([-11,1,12,1,0])

both sides of sum is not equal

False

>>>

```
private static boolean recArray ( int[] nums, int index, int sum1, int sum2 ) {
if ( index >= nums.length ) {
return sum1 == sum2;
}
int value = nums[index];
return (recArray(nums, index + 1, sum1 + value, sum2) || recArray(nums, index + 1, sum1, sum2 + value));
}
```

where index =0 at the beginning

The first method is just to simply go through the array and calculate the sums for all previous elements and all next elements, and compare them.

- sergey.a.kabanov September 18, 2016But is is too inefficient.

More efficient method is:

1). To calculate total sum of all elements.

Growing Sum =0

2). Going through the array, subtract elements from the total sum and add them to growing sum. Compare Growing Sum to the Total Sum. If they are equal - we found our element(s).