-2

I am trying find out if there is a subarray, which has the sum exactly as K. But in some its giving runtime error

this is some issue

int sum = 0, count = 0;

HashMap<Integer, Integer> hm = new HashMap<>();
hm.put(0, 1);

for (int i = 0; i < n; i++) {
    sum += arr[i];
    if (hm.containsKey(sum - k)) {
        count = hm.get(sum - k);
        count++;
        hm.put(sum, count);
    }
    else {
        hm.put(sum, 1);
    }
}

if (count > 0) {
    System.out.println("Yes");
}
else {
    System.out.println("No");
}

Thank you.

lupz
  • 3,620
  • 2
  • 27
  • 43
  • 1
    Hi and welcome to stackoverflow. Please make sure to take the [tour] and read [ask] to get started with this community. How is the code related to that screenshot? Could you please come up with an example of what you want to achieve? – lupz Jul 06 '22 at 06:34
  • First-line contains T, no of test cases. First-line of each test case contains N, the size of the array, and K. Second-line of each test case contains N spaced integers, elements of an array. sample input 3 5 3 1 2 1 3 4 4 5 1 2 1 3 3 2 1 2 1 sample output Yes No Yes and this screenshot givng runtime error in some cases – Shabbir Katlariwala Jul 06 '22 at 06:45
  • 1
    There are no linebreaks in comments. Please [edit] your question and add your inputs with proper formating. That screenshot shows that you are on some kind of learning platform. Can you ask your tutor/teacher? – lupz Jul 06 '22 at 06:53

3 Answers3

0
    int sum = 0, count = 0;

        HashMap<Integer, Integer> hm = new HashMap<>();
        hm.put(0, 1);

        for (int i = 0; i < n; i++) {
            sum += arr[i];
            if (hm.containsKey(sum - k)) {
                count = hm.get(sum - k);
                count++;
                // i think the problem is here
                hm.put(sum - k, count);
            } else {
                hm.put(sum, 1);
            }

        }

        if (count > 0) {
            System.out.println("Yes");
        } else {
            System.out.println("No");
        }
0

Well, I believe the idea of algorithm should be following:

[a1, a2, a3, ... a4, a5, a6]

sum[a3,..,a4] = sum[a1,..,a4] - sum[a1,..,a2] = k
                \_ current _/   \__ stored__/

current - k = stored

so, we are traversing array from left to right, incrementing sum of elements, checking whether (how many) we have already stored sum - k, and storing current sum in set. If that is true...

    int sum = 0, count = 0;

        HashMap<Integer, Integer> hm = new HashMap<>();
        hm.put(0, 1);

        for (int i = 0; i < n; i++) {
            sum += arr[i];
            count += hm.getOrDefault(sum - k, 0);
            hm.merge(sum, 1, Integer::sum);
        }

        if (count > 0) {
            System.out.println("Yes");
        } else {
            System.out.println("No");
        }

Andrey B. Panfilov
  • 4,324
  • 2
  • 12
  • 18
0

I see that you're using some sort of online platform to compile and run the code. Possibly, runtime error could be coming from the way you get the input. I don't see how you get the input for your code in the sample you shared. So I just compiled your logic and I get the expected output.

import java.util.HashMap;
import java.util.Scanner;

public class SubArrayTest {

    public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    int n = scanner.nextInt();
    scanner.nextLine();
    boolean doesSumExist = false;
    for (int i = 0; i < n; i++) {
        String[] arrAndK = scanner.nextLine().split(" ");
        int k = Integer.parseInt(arrAndK[1]);
        String[] inputArr = scanner.nextLine().split(" ");
        doesSumExist = findSubArraySumExists(inputArr, k);
        if (doesSumExist) {
            System.out.println("YES");
        } else {
            System.out.println("NO");
        }
    }
    scanner.close();
}

private static boolean findSubArraySumExists(String[] inputArr, int k) {
    int sum = 0, count = 0;
    HashMap<Integer, Integer> hm = new HashMap<>();
    hm.put(0, 1);

    for (int i = 0; i < inputArr.length; i++) {
        sum += Integer.parseInt(inputArr[i]);
        if (hm.containsKey(sum - k)) {
            count = hm.get(sum - k);
            count++;
            hm.put(sum, count);
        } else {
            hm.put(sum, 1);
        }
    }
    if (count > 0) {
        return true;
    }
    return false;
} 
}

Output as below

3
5 3
1 2 1 3 4
YES
4 5 
1 2 1 3
NO
3 2 
1 2 1
YES

Not sure if it helps, if not, share full code, so that we can help.

ZingAju
  • 148
  • 2
  • 13