import java.util.*;
public class Main {
public static Pair
<Integer, Integer
> findSubarraySizes
(int n,
int k,
int[] arr
) { Map
<Integer, Integer
> mp
= new HashMap
<>(); Map
<Integer, Integer
> mp2
= new HashMap
<>(); mp.put(0, 0);
int pSum = 0;
int maxLength = 0;
for (int j = 1; j <= n; j++) {
pSum += arr[j - 1];
int x = pSum - k;
if (mp.containsKey(x)) {
int i = mp.get(x) + 1;
int curLength = j - i + 1;
if (curLength > maxLength) {
maxLength = curLength;
}
}
if (mp2.containsKey(x)) {
int i = mp2.get(x) + 1;
int curLength = j - i + 1;
if (curLength < minLength) {
minLength = curLength;
}
}
mp.putIfAbsent(pSum, j);
mp2.put(pSum, j);
}
return new Pair<>(maxLength, minLength);
}
public static int countSubarraysWithLength(int n, int k, int[] arr, int targetLength) {
if (targetLength == 0) return 0;
int count = 0;
int windowSum = 0;
for (int j = 0; j < targetLength; j++) {
windowSum += arr[j];
}
if (windowSum == k) {
count++;
}
for (int j = targetLength; j < n; j++) {
windowSum += arr[j] - arr[j - targetLength];
if (windowSum == k) {
count++;
}
}
return count;
}
public static void main
(String[] args
) { int n = 6;
int k = 5;
int[] arr = {1, 2, 3, 4, 2, 5};
Pair
<Integer, Integer
> sizes
= findSubarraySizes
(n, k, arr
); int maxLength = sizes.getKey();
int minLength = sizes.getValue();
int maxCount = countSubarraysWithLength(n, k, arr, maxLength);
int minCount = countSubarraysWithLength(n, k, arr, minLength);
System.
out.
println("Max Length: " + maxLength
+ " Count: " + maxCount
); System.
out.
println("Min Length: " + minLength
+ " Count: " + minCount
); }
}
class Pair<K, V> {
private K key;
private V value;
public Pair(K key, V value) {
this.key = key;
this.value = value;
}
public K getKey() {
return key;
}
public V getValue() {
return value;
}
}
aW1wb3J0IGphdmEudXRpbC4qOwogCnB1YmxpYyBjbGFzcyBNYWluIHsKIAogICAgcHVibGljIHN0YXRpYyBQYWlyPEludGVnZXIsIEludGVnZXI+IGZpbmRTdWJhcnJheVNpemVzKGludCBuLCBpbnQgaywgaW50W10gYXJyKSB7CiAgICAgICAgTWFwPEludGVnZXIsIEludGVnZXI+IG1wID0gbmV3IEhhc2hNYXA8PigpOwogICAgICAgIE1hcDxJbnRlZ2VyLCBJbnRlZ2VyPiBtcDIgPSBuZXcgSGFzaE1hcDw+KCk7CiAgICAgICAgbXAucHV0KDAsIDApOwogICAgICAgIGludCBwU3VtID0gMDsKICAgICAgICBpbnQgbWF4TGVuZ3RoID0gMDsKICAgICAgICBpbnQgbWluTGVuZ3RoID0gSW50ZWdlci5NQVhfVkFMVUU7CiAKICAgICAgICBmb3IgKGludCBqID0gMTsgaiA8PSBuOyBqKyspIHsKICAgICAgICAgICAgcFN1bSArPSBhcnJbaiAtIDFdOwogICAgICAgICAgICBpbnQgeCA9IHBTdW0gLSBrOwogCiAgICAgICAgICAgIGlmIChtcC5jb250YWluc0tleSh4KSkgewogICAgICAgICAgICAgICAgaW50IGkgPSBtcC5nZXQoeCkgKyAxOwogICAgICAgICAgICAgICAgaW50IGN1ckxlbmd0aCA9IGogLSBpICsgMTsKICAgICAgICAgICAgICAgIGlmIChjdXJMZW5ndGggPiBtYXhMZW5ndGgpIHsKICAgICAgICAgICAgICAgICAgICBtYXhMZW5ndGggPSBjdXJMZW5ndGg7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KIAogICAgICAgICAgICBpZiAobXAyLmNvbnRhaW5zS2V5KHgpKSB7CiAgICAgICAgICAgICAgICBpbnQgaSA9IG1wMi5nZXQoeCkgKyAxOwogICAgICAgICAgICAgICAgaW50IGN1ckxlbmd0aCA9IGogLSBpICsgMTsKICAgICAgICAgICAgICAgIGlmIChjdXJMZW5ndGggPCBtaW5MZW5ndGgpIHsKICAgICAgICAgICAgICAgICAgICBtaW5MZW5ndGggPSBjdXJMZW5ndGg7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KIAogICAgICAgICAgICBtcC5wdXRJZkFic2VudChwU3VtLCBqKTsKICAgICAgICAgICAgbXAyLnB1dChwU3VtLCBqKTsKICAgICAgICB9CiAKICAgICAgICByZXR1cm4gbmV3IFBhaXI8PihtYXhMZW5ndGgsIG1pbkxlbmd0aCk7CiAgICB9CiAKICAgIHB1YmxpYyBzdGF0aWMgaW50IGNvdW50U3ViYXJyYXlzV2l0aExlbmd0aChpbnQgbiwgaW50IGssIGludFtdIGFyciwgaW50IHRhcmdldExlbmd0aCkgewogICAgICAgIGlmICh0YXJnZXRMZW5ndGggPT0gMCkgcmV0dXJuIDA7CiAgICAgICAgaW50IGNvdW50ID0gMDsKICAgICAgICBpbnQgd2luZG93U3VtID0gMDsKIAogICAgICAgIGZvciAoaW50IGogPSAwOyBqIDwgdGFyZ2V0TGVuZ3RoOyBqKyspIHsKICAgICAgICAgICAgd2luZG93U3VtICs9IGFycltqXTsKICAgICAgICB9CiAKICAgICAgICBpZiAod2luZG93U3VtID09IGspIHsKICAgICAgICAgICAgY291bnQrKzsKICAgICAgICB9CiAKICAgICAgICBmb3IgKGludCBqID0gdGFyZ2V0TGVuZ3RoOyBqIDwgbjsgaisrKSB7CiAgICAgICAgICAgIHdpbmRvd1N1bSArPSBhcnJbal0gLSBhcnJbaiAtIHRhcmdldExlbmd0aF07CiAgICAgICAgICAgIGlmICh3aW5kb3dTdW0gPT0gaykgewogICAgICAgICAgICAgICAgY291bnQrKzsKICAgICAgICAgICAgfQogICAgICAgIH0KIAogICAgICAgIHJldHVybiBjb3VudDsKICAgIH0KIAogICAgcHVibGljIHN0YXRpYyB2b2lkIG1haW4oU3RyaW5nW10gYXJncykgewogICAgICAgIGludCBuID0gNjsKICAgICAgICBpbnQgayA9IDU7CiAgICAgICAgaW50W10gYXJyID0gezEsIDIsIDMsIDQsIDIsIDV9OwogCiAgICAgICAgUGFpcjxJbnRlZ2VyLCBJbnRlZ2VyPiBzaXplcyA9IGZpbmRTdWJhcnJheVNpemVzKG4sIGssIGFycik7CiAgICAgICAgaW50IG1heExlbmd0aCA9IHNpemVzLmdldEtleSgpOwogICAgICAgIGludCBtaW5MZW5ndGggPSBzaXplcy5nZXRWYWx1ZSgpOwogCiAgICAgICAgaW50IG1heENvdW50ID0gY291bnRTdWJhcnJheXNXaXRoTGVuZ3RoKG4sIGssIGFyciwgbWF4TGVuZ3RoKTsKICAgICAgICBpbnQgbWluQ291bnQgPSBjb3VudFN1YmFycmF5c1dpdGhMZW5ndGgobiwgaywgYXJyLCBtaW5MZW5ndGgpOwogCiAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKCJNYXggTGVuZ3RoOiAiICsgbWF4TGVuZ3RoICsgIiBDb3VudDogIiArIG1heENvdW50KTsKICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oIk1pbiBMZW5ndGg6ICIgKyBtaW5MZW5ndGggKyAiIENvdW50OiAiICsgbWluQ291bnQpOwogICAgfQp9CiAKY2xhc3MgUGFpcjxLLCBWPiB7CiAgICBwcml2YXRlIEsga2V5OwogICAgcHJpdmF0ZSBWIHZhbHVlOwogCiAgICBwdWJsaWMgUGFpcihLIGtleSwgViB2YWx1ZSkgewogICAgICAgIHRoaXMua2V5ID0ga2V5OwogICAgICAgIHRoaXMudmFsdWUgPSB2YWx1ZTsKICAgIH0KIAogICAgcHVibGljIEsgZ2V0S2V5KCkgewogICAgICAgIHJldHVybiBrZXk7CiAgICB9CiAKICAgIHB1YmxpYyBWIGdldFZhbHVlKCkgewogICAgICAgIHJldHVybiB2YWx1ZTsKICAgIH0KfQ==