This commit is contained in:
2026-03-07 22:23:51 +07:00
commit 35e2bf2746
4 changed files with 49 additions and 0 deletions
Binary file not shown.
Binary file not shown.
+25
View File
@@ -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;
}
}
+24
View File
@@ -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));
}
}