Skip to content

Commit 1778e05

Browse files
author
Gonzalo Diaz
committed
[Hacker Rank] Interview Preparation Kit: Arrays: Array Manipulation. Solved ✅.
[Hacker Rank] Interview Preparation Kit: Arrays: Array Manipulation. Unit Test started.
1 parent aabab82 commit 1778e05

File tree

3 files changed

+137
-0
lines changed

3 files changed

+137
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package ae.hackerrank.interview_preparation_kit.arrays;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
7+
/**
8+
* Arrays Left Rotation.
9+
*
10+
* @link Problem definition [[docs/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.md]]
11+
*/
12+
public class CrushOptimized {
13+
14+
private CrushOptimized() {
15+
}
16+
17+
static java.util.logging.Logger logger = util.CustomLogger.getLogger();
18+
19+
/**
20+
* arrayManipulation.
21+
*/
22+
public static long arrayManipulation(int n, List<List<Integer>> queries) {
23+
// why adding 2?
24+
// first slot to adjust 1-based index and
25+
// last slot for storing accum_sum result
26+
int[] result = new int[n + 2];
27+
Arrays.fill(result, 0);
28+
int maximum = 0;
29+
30+
for (List<Integer> query : queries) {
31+
int a = query.get(0);
32+
int b = query.get(1);
33+
int k = query.get(2);
34+
35+
// Prefix
36+
result[a] += k;
37+
result[b + 1] -= k;
38+
39+
int accumSum = 0;
40+
for (int value : result) {
41+
accumSum += value;
42+
maximum = Math.max(maximum, accumSum);
43+
}
44+
}
45+
46+
return maximum;
47+
}
48+
}
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+
]

0 commit comments

Comments
 (0)