Skip to content

Commit 518311e

Browse files
author
Gonzalo Diaz
committed
[Hacker Rank] Interview Preparation Kit: Arrays: Array Manipulation. Solved ✅.
1 parent aabab82 commit 518311e

File tree

5 files changed

+260
-0
lines changed

5 files changed

+260
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package ae.hackerrank.interview_preparation_kit.arrays;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
7+
/**
8+
* Crush (Optimized).
9+
*
10+
* @link Problem definition [[docs/hackerrank/interview_preparation_kit/arrays/crush.md]]
11+
* @link Solution notes [[docs/hackerrank/interview_preparation_kit/arrays/crush_optimized-solution-notes.md]]
12+
*/
13+
public class CrushOptimized {
14+
15+
private CrushOptimized() {
16+
}
17+
18+
static java.util.logging.Logger logger = util.CustomLogger.getLogger();
19+
20+
/**
21+
* arrayManipulation.
22+
*/
23+
public static long arrayManipulation(int n, List<List<Integer>> queries) {
24+
// why adding 2?
25+
// first slot to adjust 1-based index and
26+
// last slot for storing accumSum result
27+
int[] result = new int[n + 2];
28+
Arrays.fill(result, 0);
29+
int maximum = 0;
30+
31+
for (List<Integer> query : queries) {
32+
int a = query.get(0);
33+
int b = query.get(1);
34+
int k = query.get(2);
35+
36+
// Prefix
37+
result[a] += k;
38+
result[b + 1] -= k;
39+
40+
int accumSum = 0;
41+
for (int value : result) {
42+
accumSum += value;
43+
maximum = Math.max(maximum, accumSum);
44+
}
45+
}
46+
47+
return maximum;
48+
}
49+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package ae.hackerrank.interview_preparation_kit.arrays;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import com.fasterxml.jackson.databind.ObjectMapper;
6+
import java.io.File;
7+
import java.io.IOException;
8+
import java.util.List;
9+
import org.junit.jupiter.api.BeforeAll;
10+
import org.junit.jupiter.api.Test;
11+
import org.junit.jupiter.api.TestInstance;
12+
import org.junit.jupiter.api.TestInstance.Lifecycle;
13+
14+
15+
@TestInstance(Lifecycle.PER_CLASS)
16+
class CrushTest {
17+
18+
public static class CrushTestCase {
19+
public String title;
20+
public Integer n;
21+
public List<List<Integer>> queries;
22+
public long expected;
23+
}
24+
25+
List<CrushTestCase> testCases;
26+
27+
@BeforeAll
28+
public void setup() throws IOException {
29+
ObjectMapper objectMapper = new ObjectMapper();
30+
31+
String path = String.join("/", "hackerrank",
32+
"interview_preparation_kit",
33+
"arrays",
34+
"crush.testcases.json");
35+
File file = new File(
36+
this.getClass()
37+
.getClassLoader()
38+
.getResource(path)
39+
.getFile()
40+
);
41+
42+
ObjectMapper mapper = new ObjectMapper();
43+
this.testCases = mapper.readerForListOf(CrushTestCase.class)
44+
.readValue(objectMapper.readTree(file));
45+
}
46+
47+
@Test void testArrayManipulation() {
48+
for (CrushTestCase testCase : testCases) {
49+
long solutionFound = CrushOptimized
50+
.arrayManipulation(testCase.n, testCase.queries);
51+
52+
assertEquals(testCase.expected, solutionFound,
53+
String.format("%s(%d, %s) answer must be: %s",
54+
"CrushOptimized.arrayManipulation",
55+
testCase.n,
56+
testCase.queries.toString(),
57+
testCase.expected
58+
)
59+
);
60+
}
61+
}
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
[
2+
{
3+
"title": "Sample Test Case 0",
4+
"n": 5,
5+
"queries": [[1, 2, 100],
6+
[2, 5, 100],
7+
[3, 4, 100]],
8+
"expected": 200
9+
},
10+
{
11+
"title": "Sample Test Case 1",
12+
"n": 10,
13+
"queries": [[1, 5, 3],
14+
[4, 8, 7],
15+
[6, 9, 1]],
16+
"expected": 10
17+
},
18+
{
19+
"title": "Sample Test Case 3",
20+
"n": 10,
21+
"queries": [[2, 6, 8],
22+
[3, 5, 7],
23+
[1, 8, 1],
24+
[5, 9, 15]],
25+
"expected": 31
26+
}
27+
]
Lines changed: 85 additions & 0 deletions
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` `#arrays`
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`.
Lines changed: 37 additions & 0 deletions
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` `#arrays`
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)

0 commit comments

Comments
 (0)