|
| 1 | +/** |
| 2 | + * Longest Subarray Having Sum K |
| 3 | + * Given an array of integers, find the length of the longest sub-array |
| 4 | + * having sum equal to the given value K. |
| 5 | + */ |
| 6 | + |
| 7 | +import java.io.*; |
| 8 | +import java.util.*; |
| 9 | +public class LongestSubarraySum { |
| 10 | + |
| 11 | + public static int findLongestSubarray(int[] numArray, int K) { |
| 12 | + |
| 13 | + //HashMap containing (sum, index) key:value pairs |
| 14 | + HashMap<Long, Integer> hMap = new HashMap<>(); |
| 15 | + |
| 16 | + long sum = 0l; //Stores cumulative sum as we traverse the integer array |
| 17 | + int length = 0; //Stores length of longest valid subarray |
| 18 | + |
| 19 | + for(int i=0; i<numArray.length; i++) { |
| 20 | + sum += numArray[i]; |
| 21 | + |
| 22 | + if(sum == K) |
| 23 | + length = Math.max(length, i+1); |
| 24 | + |
| 25 | + //Only the first index of occurrence of a particular sum is stored, |
| 26 | + //as we need to find length of longest valid subarray |
| 27 | + if(!hMap.containsKey(sum)) |
| 28 | + hMap.put(sum, i); |
| 29 | + |
| 30 | + //Checking if HashMap contains (sum-K) as we know that: |
| 31 | + // (sum - (sum - K)) = K, which is the required sum |
| 32 | + if(hMap.containsKey(sum - K)) |
| 33 | + length = Math.max(length, i - hMap.get(sum - K)); |
| 34 | + } |
| 35 | + |
| 36 | + return length; |
| 37 | + } |
| 38 | + |
| 39 | + public static void main(String[] args) throws IOException{ |
| 40 | + InputStreamReader read = new InputStreamReader(System.in); |
| 41 | + BufferedReader buf = new BufferedReader(read); |
| 42 | + |
| 43 | + //Taking input from user |
| 44 | + System.out.println("Enter length of array of integers: "); |
| 45 | + int len = Integer.parseInt(buf.readLine()); |
| 46 | + System.out.println("Enter array of integers: "); |
| 47 | + StringTokenizer st = new StringTokenizer(buf.readLine()); |
| 48 | + int numArray[] = new int[len]; |
| 49 | + for(int i=0; i<len; i++) { |
| 50 | + numArray[i] = Integer.parseInt(st.nextToken()); |
| 51 | + } |
| 52 | + System.out.println("Enter sum: "); |
| 53 | + int K = Integer.parseInt(buf.readLine()); |
| 54 | + |
| 55 | + System.out.print("Length of longest subarray having sum " + K + ": "); |
| 56 | + System.out.println(findLongestSubarray(numArray, K)); |
| 57 | + } |
| 58 | + |
| 59 | +} |
| 60 | + |
| 61 | +/* |
| 62 | + Time complexity: O(N) |
| 63 | + Space complexity: O(N) (for the HashMap) |
| 64 | +
|
| 65 | + TEST CASES |
| 66 | +
|
| 67 | + INPUT |
| 68 | + Enter length of array of integers: |
| 69 | + 8 |
| 70 | + Enter array of integers: |
| 71 | + 2 1 3 2 1 1 1 3 |
| 72 | + Enter sum: |
| 73 | + 4 |
| 74 | + OUTPUT |
| 75 | + Length of longest subarray having sum 4: 3 |
| 76 | +
|
| 77 | + INPUT |
| 78 | + Enter length of array of integers: |
| 79 | + 6 |
| 80 | + Enter array of integers: |
| 81 | + -5 8 -14 2 4 12 |
| 82 | + Enter sum: |
| 83 | + -5 |
| 84 | + OUTPUT |
| 85 | + Length of longest subarray having sum -5: 5 |
| 86 | +
|
| 87 | + */ |
0 commit comments