βœ…Subarray with given sum(Contest)

Subarray with given sum easy Time Limit: 2 sec Memory Limit: 128000 kB

Problem Statement :

Given an unsorted array A of size N of non-negative integers, find a continuous sub-array which adds to a given number S. Input Each test case consists of two lines. The first line of each test case is N and S, where N is the size of the array and S is the sum. The second line of each test case contains N space-separated integers denoting the array elements.

Constraints:- 1 ≀ N ≀ 105 1 ≀ Ai ≀ 105 Output Print the starting and ending positions (1 indexing) of first such occurring subarray from the left if sum equals to subarray, else print -1. Example Sample Input 5 12 1 2 3 7 5

Sample Output 2 4

Explanation: subarray starting from index 2 and ending at index 4 => {2 , 3 , 7} sum = 2 + 3 + 7 = 12

Sample Input 10 15 1 2 3 4 5 6 7 8 9 10

Sample Output 1 5

link:https://my.newtonschool.co/playground/code/91n0usto6zp4/

```java
import java.io.*; // for handling input/output
import java.util.*; // contains Collections framework

// don't change the name of this class
// you can add inner classes if needed
class Main {
    public static void main (String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        long k = scan.nextLong();
        long[] arr = new long[n];
        for(int i=0; i<n; i++){
            arr[i] = scan.nextLong();
        }
        long sum = 0;
        HashMap<Long, Integer> hm = new HashMap<>();
        boolean flag = false;
        for(int i=0; i<n; i++){
            sum += arr[i];
            if(sum == k){
                System.out.println("1 " + (i+1));
                flag = true;
                break;
            }
            else if(hm.containsKey(sum-k)){
                int start = hm.get(sum-k) + 1;
                int end = i;
                System.out.println((start + 1) + " " + (end+1));
                flag = true;
                break;
            }
            hm.put(sum,i);
        }
        if(!flag){
            System.out.println(-1);
        }

    }
}
```Subarray with given sum
easy
Time Limit: 2 sec
Memory Limit: 128000 kB

Last updated