Amazon Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: In-Person
y r u gvng jst the outline of prog..kindly post answer without errs..because it ll helpful for BEGINNERS..Any1 having BETTER solution??????
You are using middle element second time to calculate the sum of dig2. In question it is clearly mentioned that you can not use middle element twice. Not the Solution.
@Subbu2637
This is not just outline of program it is working program...... Try to run it, it will give result not error. Only things need to add is #include<stdio.h> & define SIZE with ur choice.
@DeeKaE
Yes i m using middle element but see the if condition and based on that i am calculation final sum after removing this element.
#define N 3
int main()
{
int matrix[N][N] = {.........}, sum=0,i;
input_matrix();
for(i=0;i<N;i++)
sum += matrix[i][i] + matrix[i][N-i];
if(N%2)
sum -= matrix[N/2][N/2];
printf("sum = %d", sum);
}
/* Similar, but handles the case where n == 1 a little more efficiently */
public static int
AddDiagonals2
(IntegerMatrix im)
{
int total = 0;
int low = 0;
final int n = im.matrix.length;
for (; n/2 > low; low++) {
total += im.matrix[low][low] + im.matrix[low][n-1-low];
total += im.matrix[n-1-low][low] + im.matrix[n-1-low][n-1-low];
}
if ((n & 0x1) == 1) total += im.matrix[low][low];
return total;
}
#define ROWS_COLS 5
int main()
{
int sum=0;
int a[ROWS_COLS][ROWS_COLS] = {{1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15},{16,17,18,19,20},{21,22,23,24,25}};
for(int i=0; i<ROWS_COLS ; i++)
{
if(i != (ROWS_COLS - i-1))
{
printf("Adding %d and %d to %d\n",a[i][i],a[i][ROWS_COLS-i-1],sum);
sum+=a[i][i]+a[i][ROWS_COLS-i-1];
}
else
{
printf("Adding %d to %d\n",a[i][i],sum);
sum+=a[i][i];
}
}
printf("Sum of diagnols = %d",sum);
getch();
return 0;
}
__int64 SumOfDiagonals(int* a, int n)
{
if (!a || !n) return -1;
__int64 sum = 0;
int i = 0, j = n - 1;
while (i < j)
{
sum += a[i*n + i] + a[i*n + j] + a[j*n + i] + a[j*n + j];
i++;
j--;
}
if (i == j) sum += 2 * a[i*n + i];
return sum;
}
int _tmain(int argc, _TCHAR* argv [])
{
int a [][3] = {
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
};
printf("Sum = %d\n", SumOfDiagonals((int*) a, 3));
int b [][4] = {
{ 1, 2, 3, 10 },
{ 4, 5, 6, 11 },
{ 7, 8, 9, 12 },
{ 13, 14, 15, 16 }
};
printf("Sum = %d\n", SumOfDiagonals((int*) b, 4));
return 0;
}
//Declare rows columns and arrays
int r,c;
int arr[50][50];
int i, j;
//Get no of rows
printf("Enter rows and column: ");
scanf("%d %d", &r, &c);
//Get elements of array
for(i=0;i<r;i++){
for(j=0; j<c; j++){
scanf("%d",&arr[i][j]);
}
}
//initialise sum of both the diagonals to zero so that we can pile up the data in it
int sum1=0, sum2=0;
for(i=0; i<r; i++){ //loop for rows 0 to (no_of_rows -1 )
for(j=0; j<c; j++){ //loop for column 0 to (no_of_columns -1 )
if(j==i && j==c-i-1) {sum1+=arr[i][j];break;} // both first and second diagonal intersect..
//only sum of first diag be incremented
if(j==i) sum1+=arr[i][j]; //if column is equal to row i.e. first diagonal
if(j==(c-i-1)) sum2+=arr[i][j]; //if column is equal to no_of_column-1-row i.e. second diagonal
}
}
//print out the sum
printf(" %d %d", sum1, sum2);
import java.util.Scanner;
public class sum_Diagonals {
public static void main(String[] args){
int diag1 =0,diag2=0,sum=0;
Scanner in=new Scanner(System.in);
System.out.println("Enter the Size of Square Matrix");
int m=in.nextInt();
int a[][]=new int[m][m];
System.out.println("Enter the Elements of Matrix");
for(int i=0;i<m;i++)
for(int j=0;j<m;j++)
a[i][j]=in.nextInt();
System.out.println("The Elements of Matrix is");
for(int i=0;i<m;i++){
for(int j=0;j<m;j++)
System.out.print(a[i][j]+"\t");
System.out.println();
}
for(int i=0;i<m;i++)
diag1+=a[i][i];
System.out.println("sum of diagoal 1st \n"+diag1);
for(int i=0; i<m; i++)
diag2+=a[i][m-(i+1)];
System.out.println("Sum of second diagonal \n"+ diag2);
if(m%2==1)
sum=diag1+diag2;
else
sum=diag1+diag2-a[m/2][m/2];
System.out.println("Sum of Diagonals are \n"+sum);
}
}
//you can contact me on follwing email
//abdulmutalll786@gmail.com
public class test5 {
public static void main(String[] args) {
int a[][] = {{1,2,3,4,5},{1,2,3,4,5},{1,2,3,4,5},{1,2,3,4,5},{1,2,3,4,5}};
int sum = 0;
int row = a.length;
for(int i=0,j=row-1; i<row && j>=0; i++,j--)
{
if(i!=j)
{
sum = sum+a[i][i]+a[i][j];
}
else
{
sum = sum+a[i][i];
}
}
System.out.println("diagonal sum "+sum);
}
}
public class test5 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int a[][] = {{1,2,3,4,5},{1,2,3,4,5},{1,2,3,4,5},{1,2,3,4,5},{1,2,3,4,5}};
int sum = 0;
int row = a.length;
for(int i=0,j=row-1; i<row && j>=0; i++,j--)
{
if(i!=j)
{
sum = sum+a[i][i]+a[i][j];
}
else
{
sum = sum+a[i][i];
}
}
System.out.println("diagonal sum "+sum);
}
}
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int i, j,n, dig1=0, dig2=0, sum=0;
scanf("%d",&n);
int mat[n][n] ;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&mat[i][j]);
}
}
for(i=0; i< n; i++)
{
dig1+=mat[i][i];
}
for(i=0; i< n; i++)
{
dig2+=mat[i][n-(i+1)];
}
if(n % 2)
{
sum = dig1 + dig2 - mat[n/2][n/2];
}
else
{
sum = dig1 + dig2;
}
printf("%d", sum);
return 0;
}
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int i, j,n, dig1=0, dig2=0, sum=0;
scanf("%d",&n);
int mat[n][n] ;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&mat[i][j]);
}
}
for(i=0; i< n; i++)
{
dig1+=mat[i][i];
}
for(i=0; i< n; i++)
{
dig2+=mat[i][n-(i+1)];
}
if(n % 2)
{
sum = dig1 + dig2 - mat[n/2][n/2];
}
else
{
sum = dig1 + dig2;
}
printf("%d", sum);
return 0;
}
This is very easy question....
We know that there are two diagonal of a matrix and both intersect at one(element) point if matrix is of size odd only.
Based on the above fact. Give below is working code of it.
- Rahul September 22, 2013