From cbdee6876e15832ae4b255ad37efc19d0afc7a86 Mon Sep 17 00:00:00 2001 From: Gonzalo Diaz Date: Thu, 23 May 2024 17:36:05 -0400 Subject: [PATCH 1/2] [CONFIG] Checkstyle: method names set to camelCase pattern. --- config/checkstyle/checkstyle.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/checkstyle/checkstyle.xml b/config/checkstyle/checkstyle.xml index 150b7e3..bd06ab9 100644 --- a/config/checkstyle/checkstyle.xml +++ b/config/checkstyle/checkstyle.xml @@ -348,7 +348,7 @@ - + From 87b1d002a95d1839b798bd6a52362c8390f6b156 Mon Sep 17 00:00:00 2001 From: Gonzalo Diaz Date: Thu, 23 May 2024 19:38:47 -0400 Subject: [PATCH 2/2] =?UTF-8?q?[Hacker=20Rank]:=20A=20Very=20Big=20Sum=20s?= =?UTF-8?q?olved=20=E2=9C=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/ae/hackerrank/VeryBigSum.java | 29 ++++++++++ .../java/ae/hackerrank/VeryBigSumTest.java | 23 ++++++++ docs/hackerrank/warmup/a_very_big_sum.md | 55 +++++++++++++++++++ 3 files changed, 107 insertions(+) create mode 100644 algorithm-exercises-java/src/main/java/ae/hackerrank/VeryBigSum.java create mode 100644 algorithm-exercises-java/src/test/java/ae/hackerrank/VeryBigSumTest.java create mode 100644 docs/hackerrank/warmup/a_very_big_sum.md diff --git a/algorithm-exercises-java/src/main/java/ae/hackerrank/VeryBigSum.java b/algorithm-exercises-java/src/main/java/ae/hackerrank/VeryBigSum.java new file mode 100644 index 0000000..16f445e --- /dev/null +++ b/algorithm-exercises-java/src/main/java/ae/hackerrank/VeryBigSum.java @@ -0,0 +1,29 @@ +package ae.hackerrank; + +import java.util.List; + +/** + * VeryBigSum. + * + * @link Problem definition [[docs/hackerrank/warmup/a_very_big_sum.md]] + */ +public class VeryBigSum { + + private VeryBigSum() { + } + + static java.util.logging.Logger logger = util.CustomLogger.getLogger(); + + /** + * aVeryBigSum. + */ + public static long aVeryBigSum(List ar) { + long total = 0L; + + for (long x : ar) { + total += x; + } + + return total; + } +} diff --git a/algorithm-exercises-java/src/test/java/ae/hackerrank/VeryBigSumTest.java b/algorithm-exercises-java/src/test/java/ae/hackerrank/VeryBigSumTest.java new file mode 100644 index 0000000..72239ae --- /dev/null +++ b/algorithm-exercises-java/src/test/java/ae/hackerrank/VeryBigSumTest.java @@ -0,0 +1,23 @@ +package ae.hackerrank; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.Test; + + +class VeryBigSumTest { + + @Test void test_aVeryBigSum() { + + Long answer = 5000000015L; + List arr = Arrays.asList(1000000001L, 1000000002L, 1000000003L, 1000000004L, 1000000005L); + + Long solutionFound = VeryBigSum.aVeryBigSum(arr); + + assertEquals(answer, solutionFound, + String.format("Problem 0000 answer must be: %d", answer) + ); + } +} diff --git a/docs/hackerrank/warmup/a_very_big_sum.md b/docs/hackerrank/warmup/a_very_big_sum.md new file mode 100644 index 0000000..7623241 --- /dev/null +++ b/docs/hackerrank/warmup/a_very_big_sum.md @@ -0,0 +1,55 @@ +# [A Very Big Sum](https://www.hackerrank.com/challenges/a-very-big-sum) + +Difficulty: #easy +Category: #warmup + +In this challenge, you are required to calculate and print the +sum of the elements in an array, keeping in mind that some of +those integers may be quite large. + +## Function Description + +Complete the aVeryBigSum function in the editor below. +It must return the sum of all array elements. + +aVeryBigSum has the following parameter(s): + +- int ar[n]: an array of integers. + +## Return + +- long: the sum of all array elements + +## Input Format + +The first line of the input consists of an integer n. +The next line contains space-separated integers contained in the array. + +## Output Format + +Return the integer sum of the elements in the array. + +## Constraints + +$ 1 <= n < 10 $ \ +$ 0 <= ar[i] <= 10^10 $ + +## Sample Input + +```text +5 +1000000001 1000000002 1000000003 1000000004 1000000005 +``` + +## Output + +```text +5000000015 +``` + +## Note + +The range of the 32-bit integer is +($ -2^31 $) to ($ 2^31 - 1 $) or $ [-2147483648, 2147483647] $ +When we add several integer values, the resulting sum might exceed the +above range. You might need to use long int C/C++/Java to store such sums.