Table of Contents
About The Project
It is a repository, where we produce solutions in different programming languages with different suitable for the problems found in various sources (Hackerrank, Project Euler etc.) and collect them together. 🚀
Supported Programming Languages
- C
- C#
- Java
- GoLang
Algorithms
Diagonal Difference
Given a square matrix, calculate the absolute difference between the sums of its diagonals. For example, the square matrix arr is shown below:
1 2 3
4 5 6
9 8 9
The left-to-right diagonal = 1 + 5 + 9 = 15. The right to left diagonal = 3 + 5+ 9 = 17. Their absolute difference is |15–17| = 2
Function description
Complete the diagonalDifference function in the editor below. diagonalDifference takes the following parameter:
- int arr[n][m]: an array of integers
Return
- int: the absolute diagonal difference
Input Format
The first line contains a single integer, n , the number of rows and columns in the matrix arr . Each of the next n lines describes a row, arr[i], and consists of n space-separated integers arr[i][j].
Constraints
- — 100 ≤ arr[i][j] ≤ 100
Output Format
Print the absolute difference between the sums of the matrix’s two diagonals as a single integer.
Sample Input
3
11 2 4
4 5 6
10 8 -12
Sample Output
15
Explanation
The primary diagonal is:
11
5
-12
Sum across the primary diagonal: 11 + 5 - 12 = 4
The secondary diagonal is:
4
5
10
Sum across the secondary diagonal: 4 + 5 + 10 = 19 Difference: |4 - 19| = 15
Note: |x| is the absolute value of x
Plus Minus
Given an array of integers, calculate the fractions of its elements that are positive, negative, and are zeros. Print the decimal value of each fraction on a new line.
Note: This challenge introduces precision problems. The test cases are scaled to six decimal places, though answers with absolute error of up to 10 ^ -4 are acceptable.
For example, given the array arr = [1, 1, 0, -1,-1] there are 5 elements, two positive, two negative and one zero. Their ratios would be 2/5 = 0.400000, 2/5 = 0.400000 and 1/5 = 0.200000. It should be printed as
0.400000
0.400000
0.200000
Function description
Complete the plusMinus function in the editor below. It should print out the ratio of positive, negative and zero items in the array, each on a separate line rounded to six decimals.
plusMinus has the following parameter(s):
- arr: an array of integers
Input Format
The first line contains an integer, n, denoting the size of the array. The second line contains n space-separated integers describing an array of numbers arr(arr[0], arr[1], arr[2], . . . arr[n – 1]).
Constraints
- 0 < n < 100
- – 100 < arr[i] < 100
Output Format
You must print the following 3 lines:
- A decimal representing of the fraction of positive numbers in the array compared to its size.
- A decimal representing of the fraction of negative numbers in the array compared to its size.
- A decimal representing of the fraction of zeros in the array compared to its size.
Sample Input
6
-4 3 -9 0 4 1
Sample Output
0.500000
0.333333
0.166667
Explanation
There are 3 positive numbers, 2 negative numbers, and 1 zero in the array. The proportions of occurrence are positive: 3/6 = 0.500000, negative: 2/6 = 0.333333 and zeros: 1/6 = 0.166667.