import java.util.*;
public class Main {
public static int countShortestSubarraysWithSumK(int[] nums, int k) {
int n = nums.length;
int minLength
= Integer.
MAX_VALUE, count
= 0;
for (int start = 0; start < n; start++) {
int sum = 0;
for (int end = start; end < n; end++) {
sum += nums[end];
if (sum == k) {
int length = end - start + 1;
if (length < minLength) {
minLength = length;
count = 1;
} else if (length == minLength) {
count++;
}
}
}
}
return count;
}
public static int countLargestSubarraysWithSumK(int[] nums, int k) {
int n = nums.length;
int maxLength = 0, count = 0;
for (int start = 0; start < n; start++) {
int sum = 0;
for (int end = start; end < n; end++) {
sum += nums[end];
if (sum == k) {
int length = end - start + 1;
if (length > maxLength) {
maxLength = length;
count = 1;
} else if (length == maxLength) {
count++;
}
}
}
}
return count;
}
public static void main
(String[] args
) { int[] nums = {1, 2, 3, 4, 2, 5};
int k = 5;
System.
out.
println("Shortest subarrays with sum " + k
+ ": " + countShortestSubarraysWithSumK
(nums, k
)); System.
out.
println("Largest subarrays with sum " + k
+ ": " + countLargestSubarraysWithSumK
(nums, k
)); }
}
aW1wb3J0IGphdmEudXRpbC4qOwogCnB1YmxpYyBjbGFzcyBNYWluIHsKIAogICAgcHVibGljIHN0YXRpYyBpbnQgY291bnRTaG9ydGVzdFN1YmFycmF5c1dpdGhTdW1LKGludFtdIG51bXMsIGludCBrKSB7CiAgICAgICAgaW50IG4gPSBudW1zLmxlbmd0aDsKICAgICAgICBpbnQgbWluTGVuZ3RoID0gSW50ZWdlci5NQVhfVkFMVUUsIGNvdW50ID0gMDsKIAogICAgICAgIGZvciAoaW50IHN0YXJ0ID0gMDsgc3RhcnQgPCBuOyBzdGFydCsrKSB7CiAgICAgICAgICAgIGludCBzdW0gPSAwOwogICAgICAgICAgICBmb3IgKGludCBlbmQgPSBzdGFydDsgZW5kIDwgbjsgZW5kKyspIHsKICAgICAgICAgICAgICAgIHN1bSArPSBudW1zW2VuZF07CiAgICAgICAgICAgICAgICBpZiAoc3VtID09IGspIHsKICAgICAgICAgICAgICAgICAgICBpbnQgbGVuZ3RoID0gZW5kIC0gc3RhcnQgKyAxOwogICAgICAgICAgICAgICAgICAgIGlmIChsZW5ndGggPCBtaW5MZW5ndGgpIHsKICAgICAgICAgICAgICAgICAgICAgICAgbWluTGVuZ3RoID0gbGVuZ3RoOwogICAgICAgICAgICAgICAgICAgICAgICBjb3VudCA9IDE7CiAgICAgICAgICAgICAgICAgICAgfSBlbHNlIGlmIChsZW5ndGggPT0gbWluTGVuZ3RoKSB7CiAgICAgICAgICAgICAgICAgICAgICAgIGNvdW50Kys7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgfQogCiAgICAgICAgcmV0dXJuIGNvdW50OwogICAgfQogCiAgICBwdWJsaWMgc3RhdGljIGludCBjb3VudExhcmdlc3RTdWJhcnJheXNXaXRoU3VtSyhpbnRbXSBudW1zLCBpbnQgaykgewogICAgICAgIGludCBuID0gbnVtcy5sZW5ndGg7CiAgICAgICAgaW50IG1heExlbmd0aCA9IDAsIGNvdW50ID0gMDsKIAogICAgICAgIGZvciAoaW50IHN0YXJ0ID0gMDsgc3RhcnQgPCBuOyBzdGFydCsrKSB7CiAgICAgICAgICAgIGludCBzdW0gPSAwOwogICAgICAgICAgICBmb3IgKGludCBlbmQgPSBzdGFydDsgZW5kIDwgbjsgZW5kKyspIHsKICAgICAgICAgICAgICAgIHN1bSArPSBudW1zW2VuZF07CiAgICAgICAgICAgICAgICBpZiAoc3VtID09IGspIHsKICAgICAgICAgICAgICAgICAgICBpbnQgbGVuZ3RoID0gZW5kIC0gc3RhcnQgKyAxOwogICAgICAgICAgICAgICAgICAgIGlmIChsZW5ndGggPiBtYXhMZW5ndGgpIHsKICAgICAgICAgICAgICAgICAgICAgICAgbWF4TGVuZ3RoID0gbGVuZ3RoOwogICAgICAgICAgICAgICAgICAgICAgICBjb3VudCA9IDE7CiAgICAgICAgICAgICAgICAgICAgfSBlbHNlIGlmIChsZW5ndGggPT0gbWF4TGVuZ3RoKSB7CiAgICAgICAgICAgICAgICAgICAgICAgIGNvdW50Kys7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgfQogCiAgICAgICAgcmV0dXJuIGNvdW50OwogICAgfQogCiAgICBwdWJsaWMgc3RhdGljIHZvaWQgbWFpbihTdHJpbmdbXSBhcmdzKSB7CiAgICAgICAgaW50W10gbnVtcyA9IHsxLCAyLCAzLCA0LCAyLCA1fTsKICAgICAgICBpbnQgayA9IDU7CiAKICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oIlNob3J0ZXN0IHN1YmFycmF5cyB3aXRoIHN1bSAiICsgayArICI6ICIgKyBjb3VudFNob3J0ZXN0U3ViYXJyYXlzV2l0aFN1bUsobnVtcywgaykpOwogICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbigiTGFyZ2VzdCBzdWJhcnJheXMgd2l0aCBzdW0gIiArIGsgKyAiOiAiICsgY291bnRMYXJnZXN0U3ViYXJyYXlzV2l0aFN1bUsobnVtcywgaykpOwogICAgfQp9