## Amazon Interview Question

Applications Developers**Country:**India

**Interview Type:**Written Test

nice approach to a simple question, but why do you prefer a recursive solution when you can do this in a very simple way iteratively?

Also, the original question needs some clarification, as the question seems trivial as it is asked here

Question is not quite clear, can there be an even matrix(4 x 4)? does it require sum of diagnol elements or all of the odd ordered elements?

public class diagMatrixProblem {

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

int a[][] =new int[][]{{10, 25, 56,33,172},

{74, 18, 11,139,44},

{13, 16, 17,10,55},

{71, 54, 41,13,90},

{118,35, 44, 21, 74}};

int sumOfDiagnolElems = 0,i,j;

int order =5;

for(i = 0; i < order; i++){

System.out.println("Adding elem "+ a[i][i]);

sumOfDiagnolElems += a[i][i];

}

if (order % 2 != 0){

for(i = 0,j = order -1 ; i < order && j >= 0; i++,j--){

if(i != j){

System.out.println("Adding elem "+ a[i][j]);

sumOfDiagnolElems += a[i][j];

}

}

}

System.out.println(" Sum of diagnol elems is "+ sumOfDiagnolElems);

}

}

```
public class DiagonalMatrix {
public static void main(String[]args)
{
int arr[][] = { {1, 2, 3, 10, 30}, {4, 5, 6, 11, 31}, {7, 8, 9, 12, 32}, {21, 22, 23, 13, 33}, {40, 41, 42, 43, 44} };
int col_count = arr[0].length, row_count = arr.length, sum =0;
for(int i=0;i<row_count;i++)
for(int j=0;j<col_count;j++)
{
if(i==j)
{
sum+=arr[i][j];
if(arr[i][col_count-1-j]!=arr[i][j])
sum+=arr[i][col_count-1-j];
}
}
System.out.println(sum);
}
}
```

public class DiagonalMatrix {

public static void main(String[]args)

{

int arr[][] = { {1, 2, 3, 10, 30}, {4, 5, 6, 11, 31}, {7, 8, 9, 12, 32}, {21, 22, 23, 13, 33}, {40, 41, 42, 43, 44} };

int col_count = arr[0].length, row_count = arr.length, sum =0;

for(int i=0;i<row_count;i++)

for(int j=0;j<col_count;j++)

{

if(i==j)

{

sum+=arr[i][j];

if(arr[i][col_count-1-j]!=arr[i][j])

sum+=arr[i][col_count-1-j];

}

}

System.out.println(sum);

}

}

```
public static int getSum(int [][]a){
int sum = 0;
int len = a.length;
for(int i=0,j=0 ;i<len && j < len;i++,j++){
sum += a[i][j];
if(i != len-j-1)
sum += a[i][len-j-1];
}
return sum;
}
```

```
int addDiagonals(int matrix[][])
{
int dim = findDimension(matrix);
int nIndex = dim-1;
int pIndex = 0;
int sum = 0;
int baseCursor = 0;
while(nIndex >= 0 && pIndex < dim)
{
if((matrix+baseCur+pIndex) == (matrix+baseCursor + nIndex))
sum+ = *(matrix+baseCursor+pIndex); // || In fact *(matrix+baseCursor+nIndex), both are same.
else
sum+ = *(matrix+baseCursor+pIndex) + *(matrix+baseCursor + nIndex);
pIndex++;
nIndex--;
baseCursor += dim;
}
return sum;
}
```

I think by odd-ordered, it means all elements whose indices sum to an odd value (it is my guess though which can be completely wrong). If that's the case, then we can find these elements and hash them to prevent duplicate values:

```
index=(I * row_size + j);
if(index%2 != 0)
{
val=*((int)*array+index);
if(hash[val]==-1)
{
hash[val]=1;
sum+=val;
}
}
delete [] hash;
return sum;
```

It is an O(n^2) time + O(n^2) space (worst case of all elements are unique) hence not a very efficient approach.

#include <stdio.h>

int main() {

// TODO Auto-generated method stub

int a[][5] ={{10, 25, 56,33,172},

{74, 18, 11,139,44},

{13, 16, 17,10,55},

{71, 54, 41,13,90},

{118,35, 44, 21, 74}};

int sumOfDiagnolElems = 0,i,j;

int order =5, added = 0;

for(i = 0; i < order; i++){

for(j = 0; j < order; j++) {

if (added == 0) {

added = 1;

sumOfDiagnolElems += a[i][j];

} else

added = 0;

}

}

printf(" Sum of diagnol elems is %d \n", sumOfDiagnolElems);

return 0;

}

Doing exactly what the problem statement states, here's a C implementation. I haven't figured out how to do this in Java yet...

```
#include <stdio.h>
int main(void) {
int intArray[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int oddElementsSum = addOddElements(intArray, 3, 3);
printf("The sum of odd elements is: %d\n", oddElementsSum);
return 0;
}
int addOddElements(int *integerArray, int X, int Y) {
int size = X * Y;
int sum = 0;
int i = 0;
for(i = 0; i < size; i++) {
if(((i + 1)%2) != 0) {
sum = sum + integerArray[i];
}
}
return sum;
}
```

#include <stdio.h>

#define N 3

int main(void)

{

int a[N][N], i, j, sum = 0;

printf("Enter 3*3 matrix: \n");

for (i = 0; i < N; i++)

for (j = 0; j < N; j++)

scanf("%d", &a[i][j]);

for (i = 0; i < N; i++)

for (j = 0; j < N; j++)

if ((i + j) % 2 == 0)

sum += a[i][j];

printf("\nODD ORDER SUM : %d", sum);

return 0;

}

1.consider left diagonal add the elements of left diagonal

- ram rs September 16, 20132.consider right diagonal add the elements of right diagonal

3.add sum of right diagonal and left diagonal

4.subtract the mid element a[n/2][n/2] since it has to be used only once

5.this gives the answer