Skip to content

Commit 5f89801

Browse files
authored
Merge pull request #122 from sir-gon/hackerrank
Hackerrank
2 parents f6a15e0 + d5303b3 commit 5f89801

24 files changed

+1053
-0
lines changed

docs/hackerrank/warmup/aVeryBigSum.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# [A Very Big Sum](https://www.hackerrank.com/challenges/a-very-big-sum)
2+
3+
Difficulty: #easy
4+
Category: #warmup
5+
6+
In this challenge, you are required to calculate and print the
7+
sum of the elements in an array, keeping in mind that some of
8+
those integers may be quite large.
9+
10+
## Function Description
11+
12+
Complete the aVeryBigSum function in the editor below.
13+
It must return the sum of all array elements.
14+
15+
aVeryBigSum has the following parameter(s):
16+
17+
- int ar[n]: an array of integers.
18+
19+
## Return
20+
21+
- long: the sum of all array elements
22+
23+
## Input Format
24+
25+
The first line of the input consists of an integer n.
26+
The next line contains space-separated integers contained in the array.
27+
28+
## Output Format
29+
30+
Return the integer sum of the elements in the array.
31+
32+
## Constraints
33+
34+
$ 1 <= n < 10 $ \
35+
$ 0 <= ar[i] <= 10^10 $
36+
37+
## Sample Input
38+
39+
```text
40+
5
41+
1000000001 1000000002 1000000003 1000000004 1000000005
42+
```
43+
44+
## Output
45+
46+
```text
47+
5000000015
48+
```
49+
50+
## Note
51+
52+
The range of the 32-bit integer is
53+
($ -2^31 $) to ($ 2^31 - 1 $) or $ [-2147483648, 2147483647] $
54+
When we add several integer values, the resulting sum might exceed the
55+
above range. You might need to use long int C/C++/Java to store such sums.
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# [Compare the Triplets](https://www.hackerrank.com/challenges/compare-the-triplets)
2+
3+
Difficulty: #easy
4+
Category: #warmup
5+
6+
Alice and Bob each created one problem for HackerRank. A reviewer rates the two
7+
challenges, awarding points on a scale from 1 to 100 for three categories:
8+
problem clarity, originality, and difficulty.
9+
The rating for Alice's challenge is the triplet $ a = (a[0], a[1], a[2]) $,
10+
and the rating for Bob's challenge is the triplet $ b = (b[0], b[1], b[2]) $.
11+
12+
The task is to find their comparison points by comparing $ a[0] $ with
13+
$ b[0] $, $ a[1] $ with $ b[1] $, and $ a[2] $ with $ b[2] $.
14+
15+
- If $ a[i] > b[i] $, then Alice is awarded $ 1 $ point.
16+
- If $ a[i] < b[i] $, then Bob is awarded $ 1 $ point.
17+
- If $ a[i] = b[i] $, then neither person receives a point.
18+
19+
Comparison points is the total points a person earned.
20+
Given a and b, determine their respective comparison points.
21+
22+
## Example
23+
24+
$ a = [1, 2, 3] $ \
25+
$ b = [3, 2, 1] $
26+
27+
- For elements \*0\*, Bob is awarded a point because $ a[0] $.
28+
- For the equal elements $ a[1] $ and $ b[1] $, no points are earned.
29+
- Finally, for elements $ 2 $, $ a[2] > b[2] $ so Alice receives a point.
30+
31+
The return array is $ [1, 1] $ with Alice's score first and Bob's second.
32+
33+
## Function Description
34+
35+
Complete the function compareTriplets in the editor below.
36+
compareTriplets has the following parameter(s):
37+
38+
- int a[3]: Alice's challenge rating
39+
- int b[3]: Bob's challenge rating
40+
41+
## Return
42+
43+
- int[2]: Alice's score is in the first position, and Bob's score is in the second.
44+
45+
## Input Format
46+
47+
The first line contains 3 space-separated integers, $ a[0] $, $ a[1] $, and
48+
$ a[2] $, the respective values in triplet a.
49+
The second line contains 3 space-separated integers, $ b[0] $, $ b[1] $, and
50+
$ b[2] $, the respective values in triplet b.
51+
52+
## Constraints
53+
54+
- $ 1 \leq a[i] \leq 100 $
55+
- $ 1 \leq b[i] \leq 100 $
56+
57+
## Sample Input 0
58+
59+
```text
60+
5 6 7
61+
3 6 10
62+
```
63+
64+
## Sample Output 0
65+
66+
```text
67+
1 1
68+
```
69+
70+
## Explanation 0
71+
72+
In this example:
73+
74+
- $ a = (a[0], a[1], a[2]) = (5, 6, 7) $
75+
- $ b = (b[0], b[1], b[2]) = (3, 6, 10) $
76+
77+
Now, let's compare each individual score:
78+
79+
- $ a[0] > b[0] $, so Alice receives $ 1 $ point. \
80+
- $ a[1] = b[1] $, so nobody receives a point. \
81+
- $ a[2] < b[2] $, so Bob receives $ 1 $ point.
82+
83+
Alice's comparison score is $ 1 $, and Bob's comparison score is $ 1 $.
84+
Thus, we return the array $ [1, 1] $.
85+
86+
## Sample Input 1
87+
88+
```text
89+
17 28 30
90+
99 16 8
91+
```
92+
93+
## Sample Output 1
94+
95+
```text
96+
2 1
97+
```
98+
99+
## Explanation 1
100+
101+
Comparing the *0th* elements, $ 17 < 99 $ so Bob receives a point.
102+
Comparing the *1st* and *2nd* elements $ 28 > 16 $ and $ 30 > 8 $ so Alice
103+
receives two points.
104+
The return array is $ [2, 1] $.
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# [Diagonal Difference](https://www.hackerrank.com/challenges/diagonal-difference)
2+
3+
Difficulty: #easy
4+
Category: #warmup
5+
6+
Given a square matrix, calculate the absolute difference between the sums
7+
of its diagonals.
8+
For example, the square matrix $ arr $ is shown below:
9+
10+
```text
11+
1 2 3
12+
4 5 6
13+
9 8 9
14+
```
15+
16+
The left-to-right $ diagonal = 1 + 5 + 9 = 15 $.
17+
The right to left $ diagonal = 3 + 5 + 9 = 17 $.
18+
Their absolute difference is $ |15 - 17| = 2 $.
19+
20+
## Function description
21+
22+
Complete the $ diagonalDifference $ function in the editor below.
23+
diagonalDifference takes the following parameter:
24+
25+
- int ` arr[n][m] `: an array of integers
26+
27+
## Return
28+
29+
- int: the absolute diagonal difference
30+
31+
## Input Format
32+
33+
The first line contains a single integer, n, the number of
34+
rows and columns in the square matrix arr.
35+
Each of the next n lines describes a row, arr[i], and consists of
36+
space-separated integers ` arr[i][j] `.
37+
38+
## Constraints
39+
40+
$ -100 \leq $ ` arr[i][j] ` $ \leq 100 $
41+
42+
## Output Format
43+
44+
Return the absolute difference between the sums of the matrix's
45+
two diagonals as a single integer.
46+
47+
## Sample Input
48+
49+
```text
50+
3
51+
11 2 4
52+
4 5 6
53+
10 8 -12
54+
```
55+
56+
Sample Output
57+
58+
```text
59+
15
60+
```
61+
62+
## Explanation
63+
64+
The primary diagonal is:
65+
66+
```text
67+
11
68+
5
69+
-12
70+
```
71+
72+
Sum across the primary diagonal: 11 + 5 - 12 = 4
73+
The secondary diagonal is:
74+
75+
```text
76+
4
77+
5
78+
10
79+
```
80+
81+
Sum across the secondary diagonal: $ 4 + 5 + 10 = 19 $
82+
Difference: $ |4 - 19| = 15 $
83+
84+
*Note*: $ |x| $ is the
85+
[absolute value](https://www.mathsisfun.com/numbers/absolute-value.html)
86+
of $ x $

docs/hackerrank/warmup/miniMaxSum.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# [Mini-Max Sum](https://www.hackerrank.com/challenges/mini-max-sum)
2+
3+
Difficulty: #easy
4+
Category: #warmup
5+
6+
Given five positive integers, find the minimum and maximum values
7+
that can be calculated by summing exactly four of the five integers.
8+
Then print the respective minimum and maximum values as a single line
9+
of two space-separated long integers.
10+
11+
## Example
12+
13+
$ arr = [1, 3, 5, 7, 9] $
14+
The minimum sum is $ 1 + 3 + 5 + 7 = 16 $ and the maximum sum
15+
is $ 3 + 5 + 7 + 9 = 24 $. The function prints
16+
17+
```text
18+
16 24
19+
```
20+
21+
## Function Description
22+
23+
Complete the miniMaxSum function in the editor below.
24+
miniMaxSum has the following parameter(s):
25+
26+
- arr: an array of $ 5 $ integers
27+
28+
## Print
29+
30+
Print two space-separated integers on one line: the minimum sum and
31+
the maximum sum of 4 of 5 elements.
32+
33+
## Input Format
34+
35+
A single line of five space-separated integers.
36+
37+
## Constraints
38+
39+
$ 1 \leq arra[i] \leq 10^9 $
40+
41+
## Output Format
42+
43+
Print two space-separated long integers denoting the respective minimum
44+
and maximum values that can be calculated by summing exactly four of the
45+
five integers. (The output can be greater than a 32 bit integer.)
46+
47+
## Sample Input
48+
49+
```text
50+
1 2 3 4 5
51+
```
52+
53+
## Sample Output
54+
55+
```text
56+
10 14
57+
```
58+
59+
## Explanation
60+
61+
The numbers are $ 1, 2, 3, 4, $ and $ 5 $. Calculate the following sums using
62+
four of the five integers:
63+
64+
1. Sum everything except $ 1 $, the sum is $ 2 + 3 + 4 + 5 = 14 $.
65+
2. Sum everything except $ 2 $, the sum is $ 1 + 3 + 4 + 5 = 13 $.
66+
3. Sum everything except $ 3 $, the sum is $ 1 + 2 + 4 + 5 = 12 $.
67+
4. Sum everything except $ 4 $, the sum is $ 1 + 2 + 3 + 5 = 11 $.
68+
5. Sum everything except $ 5 $, the sum is $ 1 + 2 + 3 + 4 = 10 $.
69+
70+
**Hints**: Beware of integer overflow! Use 64-bit Integer.

docs/hackerrank/warmup/plusMinus.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# [Plus Minus](https://www.hackerrank.com/challenges/plus-minus)
2+
3+
Difficulty: #easy
4+
Category: #warmup
5+
6+
Given an array of integers, calculate the ratios of its elements
7+
that are positive, negative, and zero. Print the decimal value of
8+
each fraction on a new line with 6 places after the decimal.
9+
10+
**Note**: This challenge introduces precision problems.
11+
The test cases are scaled to six decimal places, though answers
12+
with absolute error of up to $ 10^{-4} $ are acceptable.
13+
14+
## Example
15+
16+
$ arr = [1, 1, 0, -1, -1] $
17+
18+
There are $ n = 5 $ elements, two positive, two negative and one zero.
19+
Their ratios are $ 2/5 = 0.400000 $, $ 2/5 = 0.400000 $ and $ 1/5 = 0.200000 $.
20+
Results are printed as:
21+
22+
```text
23+
0.400000
24+
0.400000
25+
0.200000
26+
```
27+
28+
## Function Description
29+
30+
Complete the plusMinus function in the editor below.
31+
plusMinus has the following parameter(s):
32+
33+
- int arr[n]: an array of integers
34+
35+
## Print
36+
37+
Print the ratios of positive, negative and zero values in the array.
38+
Each value should be printed on a separate line with $ 6 $ digits after
39+
the decimal. The function should not return a value.
40+
41+
## Input Format
42+
43+
The first line contains an integer, `n`, the size of the array.
44+
The second line contains `n` space-separated integers that describe `arr[n]`.
45+
46+
## Constraints
47+
48+
$ 0 < n \leq 100 $ \
49+
$ -100 \leq arr[i] \leq 100 $
50+
51+
## Output Format
52+
53+
**Print** the following lines, each to decimals:
54+
55+
1. proportion of positive values
56+
2. proportion of negative values
57+
3. proportion of zeros
58+
59+
## Sample Input
60+
61+
```text
62+
STDIN Function
63+
----- --------
64+
6 arr[] size n = 6
65+
-4 3 -9 0 4 1 arr = [-4, 3, -9, 0, 4, 1]
66+
```
67+
68+
## Sample Output
69+
70+
```text
71+
0.500000
72+
0.333333
73+
0.166667
74+
```
75+
76+
## Explanation
77+
78+
There are $ 3 $ positive numbers, negative numbers, and $ 1 $ zero in the array.
79+
The proportions of occurrence are positive: $ 3/6 = 0.500000 $,
80+
negative: $ 2/6 = 0.333333 $ and zeros: $ 1/6 = 0.166667 $.

0 commit comments

Comments
 (0)