Skip to content

[Hacker Rank] Interview Preparation Kit: Arrays: Array Manipulation. … #213

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package ae.hackerrank.interview_preparation_kit.arrays;

import java.util.Arrays;
import java.util.List;


/**
* Arrays Left Rotation.
*
* @link Problem definition [[docs/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.md]]
*/
public class CrushOptimized {

private CrushOptimized() {
}

static java.util.logging.Logger logger = util.CustomLogger.getLogger();

/**
* arrayManipulation.
*/
public static long arrayManipulation(int n, List<List<Integer>> queries) {
// why adding 2?
// first slot to adjust 1-based index and
// last slot for storing accum_sum result
int[] result = new int[n + 2];
Arrays.fill(result, 0);
int maximum = 0;

for (List<Integer> query : queries) {
int a = query.get(0);
int b = query.get(1);
int k = query.get(2);

// Prefix
result[a] += k;
result[b + 1] -= k;

int accumSum = 0;
for (int value : result) {
accumSum += value;
maximum = Math.max(maximum, accumSum);
}
}

return maximum;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package ae.hackerrank.interview_preparation_kit.arrays;

import static org.junit.jupiter.api.Assertions.assertEquals;

import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.io.IOException;
import java.util.List;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;


@TestInstance(Lifecycle.PER_CLASS)
class CrushTest {

public static class CrushTestCase {
public String title;
public Integer n;
public List<List<Integer>> queries;
public long expected;
}

List<CrushTestCase> testCases;

@BeforeAll
public void setup() throws IOException {
ObjectMapper objectMapper = new ObjectMapper();

String path = String.join("/", "hackerrank",
"interview_preparation_kit",
"arrays",
"crush.testcases.json");
File file = new File(
this.getClass()
.getClassLoader()
.getResource(path)
.getFile()
);

ObjectMapper mapper = new ObjectMapper();
this.testCases = mapper.readerForListOf(CrushTestCase.class)
.readValue(objectMapper.readTree(file));
}

@Test void testArrayManipulation() {
for (CrushTestCase testCase : testCases) {
long solutionFound = CrushOptimized
.arrayManipulation(testCase.n, testCase.queries);

assertEquals(testCase.expected, solutionFound,
String.format("%s(%d, %s) answer must be: %s",
"CrushOptimized.arrayManipulation",
testCase.n,
testCase.queries.toString(),
testCase.expected
)
);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[
{
"title": "Sample Test Case 0",
"n": 5,
"queries": [[1, 2, 100],
[2, 5, 100],
[3, 4, 100]],
"expected": 200
},
{
"title": "Sample Test Case 1",
"n": 10,
"queries": [[1, 5, 3],
[4, 8, 7],
[6, 9, 1]],
"expected": 10
},
{
"title": "Sample Test Case 3",
"n": 10,
"queries": [[2, 6, 8],
[3, 5, 7],
[1, 8, 1],
[5, 9, 15]],
"expected": 31
}
]