import java.util.HashMap;
import java.util.Map;
public class Main {
public static int[] findLargestSubarray(int[] nums, int k) {
Map
<Integer, Integer
> sumMap
= new HashMap
<>(); sumMap.put(0, -1);
int sum = 0, maxLen = 0;
int[] result = {-1, -1};
for (int j = 0; j < nums.length; j++) {
sum += nums[j];
if (sumMap.containsKey(sum - k)) {
int i = sumMap.get(sum - k);
if (j - i > maxLen) {
maxLen = j - i;
result[0] = i + 1;
result[1] = j;
}
}
sumMap.putIfAbsent(sum, j);
}
return result;
}
public static void main
(String[] args
) { int[] nums = {1, 2, 3, 4, 5, -1, -4};
int k = 9;
int[] result = findLargestSubarray(nums, k);
if (result[0] != -1) {
System.
out.
println("Largest subarray with sum " + k
+ " is from index " + result
[0] + " to " + result
[1]); } else {
System.
out.
println("No subarray with sum " + k
); }
}
}
aW1wb3J0IGphdmEudXRpbC5IYXNoTWFwOwppbXBvcnQgamF2YS51dGlsLk1hcDsKCnB1YmxpYyBjbGFzcyBNYWluIHsKCiAgICBwdWJsaWMgc3RhdGljIGludFtdIGZpbmRMYXJnZXN0U3ViYXJyYXkoaW50W10gbnVtcywgaW50IGspIHsKICAgICAgICBNYXA8SW50ZWdlciwgSW50ZWdlcj4gc3VtTWFwID0gbmV3IEhhc2hNYXA8PigpOwogICAgICAgIHN1bU1hcC5wdXQoMCwgLTEpOwogICAgICAgIAogICAgICAgIGludCBzdW0gPSAwLCBtYXhMZW4gPSAwOwogICAgICAgIGludFtdIHJlc3VsdCA9IHstMSwgLTF9OwoKICAgICAgICBmb3IgKGludCBqID0gMDsgaiA8IG51bXMubGVuZ3RoOyBqKyspIHsKICAgICAgICAgICAgc3VtICs9IG51bXNbal07CgogICAgICAgICAgICBpZiAoc3VtTWFwLmNvbnRhaW5zS2V5KHN1bSAtIGspKSB7CiAgICAgICAgICAgICAgICBpbnQgaSA9IHN1bU1hcC5nZXQoc3VtIC0gayk7CiAgICAgICAgICAgICAgICBpZiAoaiAtIGkgPiBtYXhMZW4pIHsKICAgICAgICAgICAgICAgICAgICBtYXhMZW4gPSBqIC0gaTsKICAgICAgICAgICAgICAgICAgICByZXN1bHRbMF0gPSBpICsgMTsKICAgICAgICAgICAgICAgICAgICByZXN1bHRbMV0gPSBqOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CgogICAgICAgICAgICBzdW1NYXAucHV0SWZBYnNlbnQoc3VtLCBqKTsKICAgICAgICB9CgogICAgICAgIHJldHVybiByZXN1bHQ7CiAgICB9CgogICAgcHVibGljIHN0YXRpYyB2b2lkIG1haW4oU3RyaW5nW10gYXJncykgewogICAgICAgIGludFtdIG51bXMgPSB7MSwgMiwgMywgNCwgNSwgLTEsIC00fTsKICAgICAgICBpbnQgayA9IDk7CiAgICAgICAgaW50W10gcmVzdWx0ID0gZmluZExhcmdlc3RTdWJhcnJheShudW1zLCBrKTsKCiAgICAgICAgaWYgKHJlc3VsdFswXSAhPSAtMSkgewogICAgICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oIkxhcmdlc3Qgc3ViYXJyYXkgd2l0aCBzdW0gIiArIGsgKyAiIGlzIGZyb20gaW5kZXggIiArIHJlc3VsdFswXSArICIgdG8gIiArIHJlc3VsdFsxXSk7CiAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKCJObyBzdWJhcnJheSB3aXRoIHN1bSAiICsgayk7CiAgICAgICAgfQogICAgfQp9Cg==