Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d5b41a1

Browse files
author
Gonzalo Diaz
committed
[Hacker Rank] Interview Preparation Kit: Arrays: Array Manipulation. Solved ✅.
1 parent 79f0b42 commit d5b41a1

File tree

8 files changed

+364
-0
lines changed

8 files changed

+364
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* Crush (Brute Force).
3+
*
4+
* @link Problem definition [[docs/hackerrank/interview_preparation_kit/arrays/crush.md]]
5+
* @link Solution notes [[docs/hackerrank/interview_preparation_kit/arrays/crush_optimized-solution-notes.md]]
6+
*/
7+
8+
namespace algorithm_exercises_csharp.hackerrank.interview_preparation_kit;
9+
10+
using System.Diagnostics.CodeAnalysis;
11+
12+
public class CrushBruteForce
13+
{
14+
15+
[ExcludeFromCodeCoverage]
16+
protected CrushBruteForce() { }
17+
18+
private const int INITIALIZER = 0;
19+
20+
public static long arrayManipulation(int n, List<List<int>> queries)
21+
{
22+
// why adding 1?
23+
// first slot to adjust 1-based index and
24+
int[] result = new int[n + 1];
25+
Array.Fill(result, INITIALIZER);
26+
int maximum = INITIALIZER;
27+
28+
foreach (List<int> query in queries)
29+
{
30+
int start = query[0];
31+
int end = query[1];
32+
int value = query[2];
33+
34+
for (int i = start; i < end + 1; i++)
35+
{
36+
result[i] += value;
37+
}
38+
39+
foreach (int current in result)
40+
{
41+
maximum = Math.Max(current, maximum);
42+
}
43+
}
44+
45+
return maximum;
46+
}
47+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* Crush (Optimized).
3+
*
4+
* @link Problem definition [[docs/hackerrank/interview_preparation_kit/arrays/crush.md]]
5+
* @link Solution notes [[docs/hackerrank/interview_preparation_kit/arrays/crush_optimized-solution-notes.md]]
6+
*/
7+
8+
namespace algorithm_exercises_csharp.hackerrank.interview_preparation_kit;
9+
10+
using System.Diagnostics.CodeAnalysis;
11+
12+
public class CrushOptimized
13+
{
14+
[ExcludeFromCodeCoverage]
15+
private CrushOptimized() { }
16+
17+
/**
18+
* arrayManipulation.
19+
*/
20+
public static long arrayManipulation(int n, List<List<int>> queries)
21+
{
22+
// why adding 2?
23+
// first slot to adjust 1-based index and
24+
// last slot for storing accumSum result
25+
int[] result = new int[n + 2];
26+
Array.Fill(result, 0);
27+
int maximum = 0;
28+
29+
foreach (List<int> query in queries)
30+
{
31+
int a = query[0];
32+
int b = query[1];
33+
int k = query[2];
34+
35+
// Prefix
36+
result[a] += k;
37+
result[b + 1] -= k;
38+
39+
int accumSum = 0;
40+
foreach (int value in result)
41+
{
42+
accumSum += value;
43+
maximum = Math.Max(maximum, accumSum);
44+
}
45+
}
46+
47+
return maximum;
48+
}
49+
}
Lines changed: 73 additions & 0 deletions
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+
]

‎algorithm_exercises_csharp_test/algorithm_exercises_csharp_test.csproj‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060

6161
<ItemGroup>
6262
<EmbeddedResource Include="Resources/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.testcases.json" />
63+
<EmbeddedResource Include="Resources/hackerrank/interview_preparation_kit/arrays/crush.testcases.json" />
6364
<EmbeddedResource Include="Resources/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/ctci_ransom_note.testcases.json" />
6465
<EmbeddedResource Include="Resources/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/two_strings.testcases.json" />
6566
<EmbeddedResource Include="Resources/hackerrank/interview_preparation_kit/greedy_algorithms/luck_balance.testcases.json" />
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
namespace algorithm_exercises_csharp.hackerrank.interview_preparation_kit;
2+
3+
[TestClass]
4+
public class CrushBruteForceTest
5+
{
6+
public class CrushBruteForceTestCase
7+
{
8+
public string title { get; set; } = default!;
9+
public List<List<int>> queries { get; set; } = default!;
10+
public int n { get; set; } = default!;
11+
public long expected { get; set; } = default!;
12+
}
13+
14+
private List<CrushBruteForceTestCase> testCases { get; set; } = default!;
15+
16+
[TestInitialize]
17+
public void testInitialize()
18+
{
19+
testCases = JsonLoader.resourceLoad<List<CrushBruteForceTestCase>>(
20+
"hackerrank/interview_preparation_kit/arrays/crush.testcases.json"
21+
) ?? [];
22+
}
23+
24+
[TestMethod]
25+
public void testArrayManipulation()
26+
{
27+
long result;
28+
29+
foreach (CrushBruteForceTestCase test in testCases)
30+
{
31+
result = CrushBruteForce.arrayManipulation(test.n, test.queries);
32+
Assert.AreEqual(test.expected, result);
33+
}
34+
}
35+
}
36+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
namespace algorithm_exercises_csharp.hackerrank.interview_preparation_kit;
2+
3+
[TestClass]
4+
public class CrushOptimizedTest
5+
{
6+
public class CrushOptimizedTestCase
7+
{
8+
public string title { get; set; } = default!;
9+
public List<List<int>> queries { get; set; } = default!;
10+
public int n { get; set; } = default!;
11+
public long expected { get; set; } = default!;
12+
}
13+
14+
private List<CrushOptimizedTestCase> testCases { get; set; } = default!;
15+
16+
[TestInitialize]
17+
public void testInitialize()
18+
{
19+
testCases = JsonLoader.resourceLoad<List<CrushOptimizedTestCase>>(
20+
"hackerrank/interview_preparation_kit/arrays/crush.testcases.json"
21+
) ?? [];
22+
}
23+
24+
[TestMethod]
25+
public void testArrayManipulation()
26+
{
27+
long result;
28+
29+
foreach (CrushOptimizedTestCase test in testCases)
30+
{
31+
result = CrushOptimized.arrayManipulation(test.n, test.queries);
32+
Assert.AreEqual(test.expected, result);
33+
}
34+
}
35+
}
36+
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)

AltStyle によって変換されたページ (->オリジナル) /