commit 35e2bf274651e4af919da4f51bc6362c76a7162e Author: RedStone576 Date: Sat Mar 7 22:23:51 2026 +0700 balls diff --git a/lib/hamcrest-core-1.3.jar b/lib/hamcrest-core-1.3.jar new file mode 100644 index 0000000..9d5fe16 Binary files /dev/null and b/lib/hamcrest-core-1.3.jar differ diff --git a/lib/junit-4.13.2.jar b/lib/junit-4.13.2.jar new file mode 100644 index 0000000..6da55d8 Binary files /dev/null and b/lib/junit-4.13.2.jar differ diff --git a/src/Main.java b/src/Main.java new file mode 100644 index 0000000..82c29d3 --- /dev/null +++ b/src/Main.java @@ -0,0 +1,25 @@ +public class Main +{ + public static int smallestSubarrayLength(int[] arr, int target) + { + int smallest = 0; + + for (int i = 0; i < arr.length; i++) + { + int sum = 0; + + for (int j = i; j < arr.length; j++) + { + if (sum + arr[j] > target) break; + else if (sum + arr[j] == target) + { + smallest = j - (i - 1); + break; + } + else sum += arr[j]; + } + } + + return smallest; + } +} diff --git a/test/MainTest.java b/test/MainTest.java new file mode 100644 index 0000000..7f8589a --- /dev/null +++ b/test/MainTest.java @@ -0,0 +1,24 @@ +import org.junit.Test; +import static org.junit.Assert.*; + +public class MainTest +{ + @Test + public void test_1() + { + assertEquals(2, Main.smallestSubarrayLength(new int[]{2, 3, 1, 2, 4, 3}, 7)); + } + + @Test + public void test_2() + { + assertEquals(0, Main.smallestSubarrayLength(new int[]{1, 2, 3, 4}, 20)); + } + + @Test + public void test_3() + { + assertEquals(2, Main.smallestSubarrayLength(new int[]{1, 2, 3, 4, 5}, 9)); + } +} +