## Expedia Interview Question

Software Engineer / Developers**Country:**United States

```
class AddingArrays
{
public static void main(String[] args)
{
int[] a = {1,2,3,4};
int[] b = {9,1,7,8};
int carry = 0;
int l = a.length;
int[] result = new int[l+1];
int p = l;
result[0]=0;
for(int j=a.length-1; j>=0; j--)
{
int sum = a[j]+b[j]+carry;
int digit = sum%10;
carry = sum/10;
result[p--] = digit;
}
if(carry!=0)
result[p]=carry;
System.out.println();
for(int i=0;i<=l;i++)
System.out.print(result[i]+"\t");
System.out.println();
}
}
```

When they ask you to write code, do you have to write them on a piece of paper or you have a PC file editor?

I hate to say it but this solution works :) . At first I didn't like it because I didn't see what it did when the arrays were different sizes (and I still don't see how it takes care of this case but it works). Anyways, I did it my way (see below). I suggest others try it out. It's not as simple as the problem sounds (at least for me it wasn't). Nice job. Very compact; Good space complexity.

```
public int[] addArrays(int[] arr1, int[] arr2)
{
//make sure the arguments aren't null or empty
//return of null indicates the two array can't be added.
if(arr1 == null || arr1.length == 0)
return arr2;
if(arr2 == null || arr2.length == 0)
return arr1;
//see if the arrays are the same size
if(arr1.length != arr2.length)
{
//pad the array which has lesser elements with zero(s) to ensure the correct columns are added.
int[] temp;
if(arr1.length < arr2.length)
{
//arr1 is less
//make copy of arr1
temp = arr1;
//start index in new array
int start = arr2.length - arr1.length;
//pad with zeros; default value of int is zero.
arr1 = new int[arr2.length];
//copy original content
for (int j = start; j < arr1.length; j++)
arr1[j] = temp[j-start];
}
else
{
//arr2 is less
//make copy of arr2
temp = arr2;
//start index in new array
int start = arr1.length - arr2.length;
//pad with zeros; default value of int is zero.
arr2 = new int[arr1.length];
//copy original content
for (int j = start; j < arr2.length; j++)
arr2[j] = temp[j-start];
}
}
//arrays should be the same size at this point.
//create sum array and plus one in case there is a carry over
int[] sum = new int[arr1.length+1];
int carryOver = 0;
for (int i = arr1.length-1; i > -1; i--) {
//calculate sum on column. save it to in a variable so you wont have to keep repeating addition
int summation = carryOver + arr1[i] + arr2[i];
//store sum in array. ex. 18 mod 10 = 8 or 20 mod 10 = 0 or 25 mod 10 = 5.
sum[i+1]= summation % 10;
//calculate carry over. ex. 18 div 10 = 1 or 20 div 10 = 0 or 25 div 10 = 2.
carryOver = summation / 10;
}
//if we still have a carryover form the first indexes of arr1 and arr2 then put it in the sum array
if(carryOver > 0)
sum[0] = carryOver;
else
//step isn't necessary but if you do not want to copy an array with an extra element that fill with zero only copy what you need.
sum = Arrays.copyOfRange(sum, 1, sum.length);
return sum;
}
```

```
static void Main(string[] args)
{
// Console.Write(findPrimes(179));
// printfa();
//ninekeys();
int[] int1 = new int[4]{ 1, 2, 3, 4 };
int[] int2 = new int[4]{ 5, 6 ,7, 8};
int[] int3 = addTwo(int1, int2);
}
static int[] addTwo(int[] int1, int[] int2)
{
bool addPoint = false;
List<int> newList = new List<int>();
for (int i = int1.Length-1; i >=0; i--)
{
int newSingleValue = int1[i] + int2[i];
if (addPoint)
{
newSingleValue = newSingleValue + 1;
addPoint = false;
}
if (newSingleValue >= 10)
{
addPoint = true;
newSingleValue = newSingleValue - 10;
}
newList.Add(newSingleValue);
}
newList.Reverse();
return newList.ToArray<int>();
}
```

```
package com.dsna.puzzles;
//Given two arrays a1={1,2,3,4} and a2={5,6,7,8}, add the two array and return a new array n={6,9,1,2}
public class AddTwoArrays {
int[] a1 = new int[]{1,2,3,4};
int[] a2 = new int[]{5,6,7,8};
public static void main(String[] args) {
new AddTwoArrays().add();
}
public void add(){
StringBuffer sbf1 = new StringBuffer();
for(int i=0; i<a1.length; i++){
sbf1.append(a1[i]);
}
int int1 = Integer.parseInt(sbf1.toString());
StringBuffer sbf2 = new StringBuffer();
for(int i=0; i<a2.length; i++){
sbf2.append(a2[i]);
}
int int2 = Integer.parseInt(sbf2.toString());
String result = int1 + int2 + "";
int[] c = new int[result.length()];
for(int i=0; i<result.length();i++){
c[i] = new Integer(result.charAt(i)+"");
}
for(int i : c){
System.out.print(i+ " ");
}
}
}
```

```
public static void main(String[] args) {
int[] array1 = {1, 2, 3, 4};
int[] array2 = {5, 6, 7, 8};
int[] result = new int[array1.length];
for (int i = array1.length - 1; i >= 0; i--) {
result[i] = array1[i] + array2[i];
}
int remainder = 0;
for (int j = result.length - 1; j >= 0; j--) {
int sum = result[j] + remainder;
remainder = sum / 10;
if (sum > 9) {
result[j] = (sum) % 10;
} else {
result[j] = sum + remainder;
}
}
for(int val : result){
System.out.println(val);
}
}
```

```
public string AddNumberInArray(string s1, string s2)
{
if (s1.Length < s2.Length) return AddNumberInArray(s2, s1);
char[] s1char = s1.ToCharArray();
char[] s2char = s2.ToCharArray();
int max = s1.Length;
int min = s2.Length;
int[] maxarray = new int[max];
int[] minarray = new int[min];
maxarray = Array.ConvertAll(s1char, c => (int)Char.GetNumericValue(c));
minarray = Array.ConvertAll(s2char, c => (int)Char.GetNumericValue(c));
int carry = 0;
int[] result = new int[max+1];
int minindexstop = 0;
int sum = 0;
for (int i = 1; i <= min; i++)
{
sum = minarray[minarray.Length - i] + maxarray[maxarray.Length - i] + carry;
result[result.Length - i] = sum%10;
carry = sum / 10;
minindexstop = i;
}
for(; minindexstop < result.Length-1; minindexstop++ )
{
sum = maxarray[maxarray.Length - minindexstop-1] + carry;
result[result.Length - minindexstop - 1] = sum%10;
carry = sum / 10;
}
result[0] = carry;
string strresult = string.Join("", result);
if (strresult.StartsWith("0")) return strresult.Substring(1);
return strresult;
}
```

import java.util.Scanner;

public class ArraysSumOfTwoArrays {

public static void main(String[] args) {

Scanner sn = new Scanner(System.in);

int first = sn.nextInt();

int[] arr1 = new int[first];

for (int i = 0; i < arr1.length; i++) {

arr1[i] = sn.nextInt();

}

int second = sn.nextInt();

int[] arr2 = new int[second];

for (int i = 0; i < arr2.length; i++) {

arr2[i] = sn.nextInt();

}

int[] Result = addArray(arr1, arr2);

for (int i = 0; i < Result.length; i++) {

if(Result[i]==0 && i==0) {

}else {

System.out.print(Result[i]+", ");

}

}

System.out.print("END");

}

public static int[] addArray(int[] arr1, int arr2[]) {

int firstlength = arr1.length - 1;

int secondlength = arr2.length - 1;

int counter;

if (firstlength > secondlength) {

counter = firstlength + 1;

} else {

counter = secondlength + 1;

}

int[] result = new int[counter + 1];

int carry = 0;

while (counter > -1) {

if (firstlength >= 0) {

carry = carry + arr1[firstlength--];

}

if (secondlength >= 0) {

carry = carry + arr2[secondlength--];

}

result[counter--] = carry % 10;

carry = carry / 10;

}

return result;

}

}

I hope It Helps You Guys

- careerup007tm May 26, 2012