Skip to content

Commit 62013ee

Browse files
author
Gonzalo Diaz
committed
[Hacker Rank] Interview Preparation Kit: Arrays: Array Manipulation. Solved ✅. Optimized solution.
1 parent 7d9e56b commit 62013ee

File tree

3 files changed

+129
-0
lines changed

3 files changed

+129
-0
lines changed
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 `
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)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* @link Problem definition [[docs/hackerrank/interview_preparation_kit/2d_array.md]]
3+
*/
4+
5+
package hackerrank
6+
7+
func arrayManipulation(n int32, queries [][]int32) int64 {
8+
// why adding 2?
9+
// first slot to adjust 1-based index and
10+
// last slot for storing accumSum result
11+
var LENGTH int32 = n + 2
12+
const InitialValue int64 = 0
13+
var maximum int64 = 0
14+
15+
result := make([]int64, LENGTH)
16+
for i := 0; int32(i) < LENGTH; i++ {
17+
result[i] = InitialValue
18+
}
19+
20+
var aStart int32
21+
var bEnd int32
22+
var kValue int32
23+
24+
for _, query_row := range queries {
25+
aStart = query_row[0]
26+
bEnd = query_row[1]
27+
kValue = query_row[2]
28+
29+
result[aStart] += int64(kValue)
30+
result[bEnd+1] -= int64(kValue)
31+
}
32+
33+
var accumSum int64 = 0
34+
35+
for _, value := range result {
36+
accumSum += value
37+
if accumSum > maximum {
38+
maximum = accumSum
39+
}
40+
}
41+
42+
return maximum
43+
}
44+
45+
func ArrayManipulation(n int32, queries [][]int32) int64 {
46+
return arrayManipulation(n, queries)
47+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package hackerrank
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
"gon.cl/algorithms/utils"
10+
)
11+
12+
type CrushTestCase struct {
13+
N int32 `json:"n"`
14+
Queries [][]int32 `json:"queries"`
15+
Expected int64 `json:"expected"`
16+
}
17+
18+
var CrushTestCases []CrushTestCase
19+
20+
// You can use testing.T, if you want to test the code without benchmarking
21+
func CrushSetupSuite(t testing.TB) {
22+
wd, _ := os.Getwd()
23+
filepath := wd + "/crush.testcases.json"
24+
t.Log("Setup test cases from JSON: ", filepath)
25+
26+
var _, err = utils.LoadJSON(filepath, &CrushTestCases)
27+
if err != nil {
28+
t.Log(err)
29+
}
30+
}
31+
32+
func TestCrush(t *testing.T) {
33+
34+
CrushSetupSuite(t)
35+
36+
for _, tt := range CrushTestCases {
37+
testname := fmt.Sprintf("ArrayManipulation(%d, %v) => %d \n", tt.N, tt.Queries, tt.Expected)
38+
t.Run(testname, func(t *testing.T) {
39+
40+
ans := ArrayManipulation(tt.N, tt.Queries)
41+
assert.Equal(t, tt.Expected, ans)
42+
})
43+
44+
}
45+
}

0 commit comments

Comments
 (0)