Skip to content

Commit 8b90f90

Browse files
authored
Merge pull request #467 from sir-gon/feature/crush
[Hacker Rank] Interview Preparation Kit: Arrays: Array Manipulation. …
2 parents 6ea55d0 + 9a87f87 commit 8b90f90

File tree

7 files changed

+301
-0
lines changed

7 files changed

+301
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# [Arrays: Array Manipulation](https://www.hackerrank.com/challenges/crush)
2+
3+
Perform m operations on an array and print the maximum of the values.
4+
5+
- Difficulty: ` #hard `
6+
- Category: ` #ProblemSolvingIntermediate `
7+
8+
Starting with a 1-indexed array of zeros and a list of operations, for each
9+
operation add a value to each the array element between two given indices,
10+
inclusive. Once all operations have been performed, return the maximum
11+
value in the array.
12+
13+
## Example
14+
15+
Queries are interpreted as follows:
16+
17+
```text
18+
a b k
19+
1 5 3
20+
4 8 7
21+
6 9 1
22+
```
23+
24+
Add the values of between the indices and inclusive:
25+
26+
```text
27+
index-> 1 2 3 4 5 6 7 8 9 10
28+
[0,0,0, 0, 0,0,0,0,0, 0]
29+
[3,3,3, 3, 3,0,0,0,0, 0]
30+
[3,3,3,10,10,7,7,7,0, 0]
31+
[3,3,3,10,10,8,8,8,1, 0]
32+
```
33+
34+
The largest value is `10` after all operations are performed.
35+
36+
## Function Description
37+
38+
Complete the function arrayManipulation in the editor below.
39+
40+
arrayManipulation has the following parameters:
41+
42+
- `int n` - the number of elements in the array
43+
- `int queries[q][3]` - a two dimensional array of queries where
44+
each `queries[i]` contains three integers, `a`, `b`, and `k`.
45+
46+
## Returns
47+
48+
- int - the maximum value in the resultant array
49+
50+
## Input Format
51+
52+
The first line contains two space-separated integers `n` and `m`, the size of
53+
the array and the number of operations.
54+
Each of the next `m` lines contains three space-separated integers
55+
`a`, `b` and `k`, the left index, right index and summand.
56+
57+
## Constraints
58+
59+
- $ 3 \leq n \leq 10^7 $
60+
- $ 1 \leq m \leq 2*10^5 $
61+
- $ 1 \leq a \leq b \leq n $
62+
- $ 0 \leq k \leq 10^9 $
63+
64+
## Sample Input
65+
66+
```text
67+
5 3
68+
1 2 100
69+
2 5 100
70+
3 4 100
71+
```
72+
73+
## Sample Output
74+
75+
```text
76+
200
77+
````
78+
79+
## Explanation
80+
81+
After the first update the list is `100 100 0 0 0`.
82+
After the second update list is `100 200 100 100 100`.
83+
After the third update list is `100 200 200 200 100`.
84+
85+
The maximum value is `200`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# [Array Manipulation](https://www.hackerrank.com/challenges/crush)
2+
3+
Perform m operations on an array and print the maximum of the values.
4+
5+
- Difficulty: ` #hard `
6+
- Category: ` #ProblemSolvingIntermediate `
7+
8+
## Solution sources
9+
10+
### Brute force idea
11+
12+
The first solution attempt is based on the idea of going through:
13+
14+
> each row and then,
15+
> > each sub-set of elements affected by the operation.
16+
17+
With this principle, the algorithm becomes O(N^2)
18+
19+
### Optimized
20+
21+
Reading about posible optimizations,
22+
I found the possibility of summarizing the interior traversal with
23+
addition operations for each element in each row of operations,
24+
in only 2 constant operations, which represents the necessary values so that
25+
in a single final traversal, the sum values can be obtained "by drag".
26+
The algorithm is called "prefix sum."
27+
28+
Some sources about "prefix sum"
29+
30+
- <https://hmn.wiki/en/Prefix_sum>
31+
- <https://en.wikipedia.org/wiki/Prefix_sum>
32+
- <https://usaco.guide/silver/prefix-sums?lang=py>
33+
34+
Some sources about implementation in:
35+
36+
- [HackerRank Array Manipulation — beat the clock using Prefix Sum (JavaScript)](https://medium.com/@mlgerardvla/hackerrank-array-manipulation-beat-the-clock-using-prefix-sum-92471060035e)
37+
- [Hackerrank Discussions Forums: Array Manipulation](https://www.hackerrank.com/challenges/one-month-preparation-kit-crush/forum)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @link Problem definition [[docs/hackerrank/interview_preparation_kit/arrays/crush.md]]
3+
* @see Solution Notes: [[docs/hackerrank/interview_preparation_kit/arrays/crush_optimized-solution-notes.md]]
4+
*/
5+
6+
import { logger as console } from '../../../logger.js';
7+
8+
export function arrayManipulation(n, queries) {
9+
const LENGTH = n + 1;
10+
const SURROGATE_VALUE = 0;
11+
const result = Array(LENGTH).fill(SURROGATE_VALUE);
12+
let maximum = 0;
13+
14+
queries.forEach((query) => {
15+
const [aStart, bEnd, kValue] = query;
16+
console.debug(`start -> ${result}`);
17+
18+
for (let i = aStart; i <= bEnd; i++) {
19+
result[i] += kValue;
20+
console.debug(`result -> ${result}`);
21+
}
22+
});
23+
24+
result.forEach((value) => {
25+
maximum = Math.max(value, maximum);
26+
});
27+
28+
return maximum;
29+
}
30+
31+
export default { arrayManipulation };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { describe, expect, it } from '@jest/globals';
2+
import { logger as console } from '../../../logger.js';
3+
4+
import TEST_CASES from './cruch_testcases_test.json';
5+
6+
import { arrayManipulation } from './cruch_bruteforce.js';
7+
8+
describe('arrays: crush (bruteforce) small cases', () => {
9+
it('arrayManipulation Test Cases', () => {
10+
expect.assertions(3);
11+
12+
TEST_CASES.forEach((test) => {
13+
const answer = arrayManipulation(test.n, test.queries);
14+
15+
console.debug(
16+
`rotLeftOne(${test.n}, ${test.queries}) solution found: ${answer}`
17+
);
18+
19+
expect(answer).toStrictEqual(test.expected);
20+
});
21+
});
22+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @link Problem definition [[docs/hackerrank/interview_preparation_kit/arrays/crush.md]]
3+
*/
4+
5+
export function arrayManipulation(n, queries) {
6+
// why adding 2?
7+
// first slot to adjust 1-based index and
8+
// last slot for storing accumSum result
9+
const LENGTH = n + 2;
10+
const INITIAL_VALUE = 0;
11+
const result = Array(LENGTH).fill(INITIAL_VALUE);
12+
let maximum = 0;
13+
14+
queries.forEach((query) => {
15+
const [aStart, bEnd, kValue] = query;
16+
17+
result[aStart] += kValue;
18+
result[bEnd + 1] -= kValue;
19+
});
20+
21+
let accumSum = 0;
22+
23+
result.forEach((value) => {
24+
accumSum += value;
25+
maximum = Math.max(maximum, accumSum);
26+
});
27+
28+
return maximum;
29+
}
30+
31+
export default { arrayManipulation };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { describe, expect, it } from '@jest/globals';
2+
import { logger as console } from '../../../logger.js';
3+
4+
import TEST_CASES from './cruch_testcases_test.json';
5+
6+
import { arrayManipulation } from './cruch_optimized.js';
7+
8+
describe('arrays: crush (optimized)', () => {
9+
it('arrayManipulation Test Cases', () => {
10+
expect.assertions(3);
11+
12+
TEST_CASES.forEach((test) => {
13+
const answer = arrayManipulation(test.n, test.queries);
14+
15+
console.debug(
16+
`rotLeftOne(${test.n}, ${test.queries}) solution found: ${answer}`
17+
);
18+
19+
expect(answer).toStrictEqual(test.expected);
20+
});
21+
});
22+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
[
2+
{
3+
"title": "Sample Test Case 0",
4+
"n": 5,
5+
"queries": [
6+
[
7+
1,
8+
2,
9+
100
10+
],
11+
[
12+
2,
13+
5,
14+
100
15+
],
16+
[
17+
3,
18+
4,
19+
100
20+
]
21+
],
22+
"expected": 200
23+
},
24+
{
25+
"title": "Sample Test Case 1",
26+
"n": 10,
27+
"queries": [
28+
[
29+
1,
30+
5,
31+
3
32+
],
33+
[
34+
4,
35+
8,
36+
7
37+
],
38+
[
39+
6,
40+
9,
41+
1
42+
]
43+
],
44+
"expected": 10
45+
},
46+
{
47+
"title": "Sample Test Case 3",
48+
"n": 10,
49+
"queries": [
50+
[
51+
2,
52+
6,
53+
8
54+
],
55+
[
56+
3,
57+
5,
58+
7
59+
],
60+
[
61+
1,
62+
8,
63+
1
64+
],
65+
[
66+
5,
67+
9,
68+
15
69+
]
70+
],
71+
"expected": 31
72+
}
73+
]

0 commit comments

Comments
 (0)