diff --git a/algorithm-exercises-java/src/main/java/ae/hackerrank/interview_preparation_kit/arrays/CrushOptimized.java b/algorithm-exercises-java/src/main/java/ae/hackerrank/interview_preparation_kit/arrays/CrushOptimized.java new file mode 100644 index 0000000..fe75cac --- /dev/null +++ b/algorithm-exercises-java/src/main/java/ae/hackerrank/interview_preparation_kit/arrays/CrushOptimized.java @@ -0,0 +1,49 @@ +package ae.hackerrank.interview_preparation_kit.arrays; + +import java.util.Arrays; +import java.util.List; + + +/** + * Crush (Optimized). + * + * @link Problem definition [[docs/hackerrank/interview_preparation_kit/arrays/crush.md]] + * @link Solution notes [[docs/hackerrank/interview_preparation_kit/arrays/crush_optimized-solution-notes.md]] + */ +public class CrushOptimized { + + private CrushOptimized() { + } + + static java.util.logging.Logger logger = util.CustomLogger.getLogger(); + + /** + * arrayManipulation. + */ + public static long arrayManipulation(int n, List> queries) { + // why adding 2? + // first slot to adjust 1-based index and + // last slot for storing accumSum result + int[] result = new int[n + 2]; + Arrays.fill(result, 0); + int maximum = 0; + + for (List 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; + } +} diff --git a/algorithm-exercises-java/src/test/java/ae/hackerrank/interview_preparation_kit/arrays/CrushTest.java b/algorithm-exercises-java/src/test/java/ae/hackerrank/interview_preparation_kit/arrays/CrushTest.java new file mode 100644 index 0000000..3bd9af5 --- /dev/null +++ b/algorithm-exercises-java/src/test/java/ae/hackerrank/interview_preparation_kit/arrays/CrushTest.java @@ -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> queries; + public long expected; + } + + List 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 + ) + ); + } + } +} diff --git a/algorithm-exercises-java/src/test/resources/hackerrank/interview_preparation_kit/arrays/crush.testcases.json b/algorithm-exercises-java/src/test/resources/hackerrank/interview_preparation_kit/arrays/crush.testcases.json new file mode 100644 index 0000000..0ba0fd9 --- /dev/null +++ b/algorithm-exercises-java/src/test/resources/hackerrank/interview_preparation_kit/arrays/crush.testcases.json @@ -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 + } +] diff --git a/docs/hackerrank/interview_preparation_kit/arrays/crush.md b/docs/hackerrank/interview_preparation_kit/arrays/crush.md new file mode 100644 index 0000000..5cd1f3b --- /dev/null +++ b/docs/hackerrank/interview_preparation_kit/arrays/crush.md @@ -0,0 +1,85 @@ +# [Arrays: Array Manipulation](https://www.hackerrank.com/challenges/crush) + +Perform m operations on an array and print the maximum of the values. + +- Difficulty: `#hard` +- Category: `#ProblemSolvingIntermediate` `#arrays` + +Starting with a 1-indexed array of zeros and a list of operations, for each +operation add a value to each the array element between two given indices, +inclusive. Once all operations have been performed, return the maximum +value in the array. + +## Example + +Queries are interpreted as follows: + +```text + a b k + 1 5 3 + 4 8 7 + 6 9 1 +``` + +Add the values of between the indices and inclusive: + +```text +index-> 1 2 3 4 5 6 7 8 9 10 + [0,0,0, 0, 0,0,0,0,0, 0] + [3,3,3, 3, 3,0,0,0,0, 0] + [3,3,3,10,10,7,7,7,0, 0] + [3,3,3,10,10,8,8,8,1, 0] +``` + +The largest value is `10` after all operations are performed. + +## Function Description + +Complete the function arrayManipulation in the editor below. + +arrayManipulation has the following parameters: + +- `int n` - the number of elements in the array +- `int queries[q][3]` - a two dimensional array of queries where +each `queries[i]` contains three integers, `a`, `b`, and `k`. + +## Returns + +- int - the maximum value in the resultant array + +## Input Format + +The first line contains two space-separated integers `n` and `m`, the size of +the array and the number of operations. +Each of the next `m` lines contains three space-separated integers +`a`, `b` and `k`, the left index, right index and summand. + +## Constraints + +- $ 3 \leq n \leq 10^7 $ +- $ 1 \leq m \leq 2*10^5 $ +- $ 1 \leq a \leq b \leq n $ +- $ 0 \leq k \leq 10^9 $ + +## Sample Input + +```text +5 3 +1 2 100 +2 5 100 +3 4 100 +``` + +## Sample Output + +```text +200 +```` + +## Explanation + +After the first update the list is `100 100 0 0 0`. +After the second update list is `100 200 100 100 100`. +After the third update list is `100 200 200 200 100`. + +The maximum value is `200`. diff --git a/docs/hackerrank/interview_preparation_kit/arrays/crush_optimized-solution-notes.md b/docs/hackerrank/interview_preparation_kit/arrays/crush_optimized-solution-notes.md new file mode 100644 index 0000000..72141a5 --- /dev/null +++ b/docs/hackerrank/interview_preparation_kit/arrays/crush_optimized-solution-notes.md @@ -0,0 +1,37 @@ +# [Array Manipulation](https://www.hackerrank.com/challenges/crush) + +Perform m operations on an array and print the maximum of the values. + +- Difficulty: `#hard` +- Category: `#ProblemSolvingIntermediate` `#arrays` + +## Solution sources + +### Brute force idea + +The first solution attempt is based on the idea of going through: + +> each row and then, +> > each sub-set of elements affected by the operation. + +With this principle, the algorithm becomes O(N^2) + +### Optimized + +Reading about posible optimizations, +I found the possibility of summarizing the interior traversal with +addition operations for each element in each row of operations, +in only 2 constant operations, which represents the necessary values so that +in a single final traversal, the sum values can be obtained "by drag". +The algorithm is called "prefix sum." + +Some sources about "prefix sum" + +- +- +- + +Some sources about implementation in: + +- [HackerRank Array Manipulation — beat the clock using Prefix Sum (JavaScript)](https://medium.com/@mlgerardvla/hackerrank-array-manipulation-beat-the-clock-using-prefix-sum-92471060035e) +- [Hackerrank Discussions Forums: Array Manipulation](https://www.hackerrank.com/challenges/one-month-preparation-kit-crush/forum)