0% found this document useful (0 votes)
2 views6 pages

Day 1 Answer Key

The document provides implementations of a function named 'diagonalDifference' in Python, Java, and C, which calculates the absolute difference between the sums of the primary and secondary diagonals of a square matrix. Each implementation reads a 2D array as input and returns the computed difference as an integer. The code includes necessary input handling and memory management for the C version.

Uploaded by

islamsmytheen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views6 pages

Day 1 Answer Key

The document provides implementations of a function named 'diagonalDifference' in Python, Java, and C, which calculates the absolute difference between the sums of the primary and secondary diagonals of a square matrix. Each implementation reads a 2D array as input and returns the computed difference as an integer. The code includes necessary input handling and memory management for the C version.

Uploaded by

islamsmytheen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

USING PYTHON​

#!/bin/python3
import math
import os
import random
import re
import sys

#
# Complete the 'diagonalDifference' function below.
#
# The function is expected to return an INTEGER.
# The function accepts 2D_INTEGER_ARRAY arr as parameter.
#

def diagonalDifference(arr):
# Write your code here
sum1=0
sum2=0

n=len(arr)
for i in range(n):
sum1 = sum1 + arr[i][i] #primary diagonal
for i in range(n):
sum2= sum2 + arr[i][n-i-1] #secondary diagonal
res = sum1- sum2
return int(math.fabs(res)) #finding absolute value and converting it to integer, by default- 'fabs'
returns decimal

if _name_ == '_main_':
fptr = open(os.environ['OUTPUT_PATH'], 'w')

n = int(input().strip())

arr = []

for _ in range(n):
arr.append(list(map(int, input().rstrip().split())))

result = diagonalDifference(arr)

fptr.write(str(result) + '\n')

fptr.close()
USING JAVA 8

public static int diagonalDifference(List<List<Integer>> arr) {


int n = arr.size();
int primaryDiagonal = 0;
int secondaryDiagonal = 0;

for (int i = 0; i < n; i++) {


primaryDiagonal += arr.get(i).get(i); // top-left to bottom-right
secondaryDiagonal += arr.get(i).get(n - 1 - i); // top-right to bottom-left
}

return Math.abs(primaryDiagonal - secondaryDiagonal);


}

USING C

#include <assert.h>
#include <ctype.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char* readline();
char* ltrim(char*);
char* rtrim(char*);
char** split_string(char*);

int parse_int(char*);

/*
* Complete the 'diagonalDifference' function below.
*
* The function is expected to return an INTEGER.
* The function accepts 2D_INTEGER_ARRAY arr as parameter.
*/

int diagonalDifference(int arr_rows, int arr_columns, int** arr) {


int primary_diag = 0;
int secondary_diag = 0;

for (int i = 0; i < arr_rows; i++) {


primary_diag += arr[i][i]; // top-left to bottom-right
secondary_diag += arr[i][arr_columns - i - 1]; // top-right to bottom-left
}

return abs(primary_diag - secondary_diag);


}

int main()
{
FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w");

int n = parse_int(ltrim(rtrim(readline())));

int** arr = malloc(n * sizeof(int*));

for (int i = 0; i < n; i++) {


*(arr + i) = malloc(n * (sizeof(int)));

char** arr_item_temp = split_string(rtrim(readline()));

for (int j = 0; j < n; j++) {


int arr_item = parse_int(*(arr_item_temp + j));

*(*(arr + i) + j) = arr_item;
}
}

int result = diagonalDifference(n, n, arr);

fprintf(fptr, "%d\n", result);

fclose(fptr);

return 0;
}

char* readline() {
size_t alloc_length = 1024;
size_t data_length = 0;
char* data = malloc(alloc_length);

while (true) {
char* cursor = data + data_length;
char* line = fgets(cursor, alloc_length - data_length, stdin);

if (!line) {
break;
}

data_length += strlen(cursor);

if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') {


break;
}

alloc_length <<= 1;

data = realloc(data, alloc_length);

if (!data) {
data = '\0';
break;
}
}

if (data[data_length - 1] == '\n') {
data[data_length - 1] = '\0';

data = realloc(data, data_length);

if (!data) {
data = '\0';
}
} else {
data = realloc(data, data_length + 1);

if (!data) {
data = '\0';
} else {
data[data_length] = '\0';
}
}
return data;
}

char* ltrim(char* str) {


if (!str) {
return '\0';
}

if (!*str) {
return str;
}

while (*str != '\0' && isspace(*str)) {


str++;
}

return str;
}

char* rtrim(char* str) {


if (!str) {
return '\0';
}

if (!*str) {
return str;
}

char* end = str + strlen(str) - 1;

while (end >= str && isspace(*end)) {


end--;
}

*(end + 1) = '\0';

return str;
}

char** split_string(char* str) {


char** splits = NULL;
char* token = strtok(str, " ");

int spaces = 0;
while (token) {
splits = realloc(splits, sizeof(char*) * ++spaces);

if (!splits) {
return splits;
}

splits[spaces - 1] = token;

token = strtok(NULL, " ");


}

return splits;
}

int parse_int(char* str) {


char* endptr;
int value = strtol(str, &endptr, 10);

if (endptr == str || *endptr != '\0') {


exit(EXIT_FAILURE);
}

return value;
}

You might also like