import java.util.*;
public class Main {
public static int[] largestSubarraySumK(int[] nums, int k) {
int maxLen = 0;
int[] result = {-1, -1};
for (int i = 0; i < nums.length; ++i) {
int currentSum = 0;
for (int j = i; j < nums.length; ++j) {
currentSum += nums[j];
if (currentSum == k && (j - i + 1) > maxLen) {
maxLen = j - i + 1;
result[0] = i + 1; // 1-based index
result[1] = j + 1;
}
}
}
return result;
}
public static void main
(String[] args
) { int[] nums = {1, 2, 3, 4, 5, -1, 6};
int k = 9;
int[] result = largestSubarraySumK(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
); }
}
}
aW1wb3J0IGphdmEudXRpbC4qOwogCnB1YmxpYyBjbGFzcyBNYWluIHsKICAgIHB1YmxpYyBzdGF0aWMgaW50W10gbGFyZ2VzdFN1YmFycmF5U3VtSyhpbnRbXSBudW1zLCBpbnQgaykgewogICAgICAgIGludCBtYXhMZW4gPSAwOwogICAgICAgIGludFtdIHJlc3VsdCA9IHstMSwgLTF9OwogCiAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCBudW1zLmxlbmd0aDsgKytpKSB7CiAgICAgICAgICAgIGludCBjdXJyZW50U3VtID0gMDsKICAgICAgICAgICAgZm9yIChpbnQgaiA9IGk7IGogPCBudW1zLmxlbmd0aDsgKytqKSB7CiAgICAgICAgICAgICAgICBjdXJyZW50U3VtICs9IG51bXNbal07CiAgICAgICAgICAgICAgICBpZiAoY3VycmVudFN1bSA9PSBrICYmIChqIC0gaSArIDEpID4gbWF4TGVuKSB7CiAgICAgICAgICAgICAgICAgICAgbWF4TGVuID0gaiAtIGkgKyAxOwogICAgICAgICAgICAgICAgICAgIHJlc3VsdFswXSA9IGkgKyAxOyAgLy8gMS1iYXNlZCBpbmRleAogICAgICAgICAgICAgICAgICAgIHJlc3VsdFsxXSA9IGogKyAxOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIHJldHVybiByZXN1bHQ7CiAgICB9CiAKICAgIHB1YmxpYyBzdGF0aWMgdm9pZCBtYWluKFN0cmluZ1tdIGFyZ3MpIHsKICAgICAgICBpbnRbXSBudW1zID0gezEsIDIsIDMsIDQsIDUsIC0xLCA2fTsKICAgICAgICBpbnQgayA9IDk7CiAgICAgICAgaW50W10gcmVzdWx0ID0gbGFyZ2VzdFN1YmFycmF5U3VtSyhudW1zLCBrKTsKICAgICAgICBpZiAocmVzdWx0WzBdICE9IC0xKSB7CiAgICAgICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbigiTGFyZ2VzdCBzdWJhcnJheSB3aXRoIHN1bSAiICsgayArICIgaXMgZnJvbSBpbmRleCAiICsgcmVzdWx0WzBdICsgIiB0byAiICsgcmVzdWx0WzFdKTsKICAgICAgICB9IGVsc2UgewogICAgICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oIk5vIHN1YmFycmF5IHdpdGggc3VtICIgKyBrKTsKICAgICAgICB9CiAgICB9Cn0=